English 中文(简体)
优化公共卫生和社会福利部中的这一成绩
原标题:Optimize this Query in PHP
  • 时间:2011-11-03 14:39:18
  •  标签:
  • php
  • mysql

首先,我没有控制数据库结构等。

我需要使用PHP,以逐国和国名检索记录,但所有数据都在多个表格中。 基本上,我需要尝试和理解如何把这两个问题结合起来,以便它们不会陷入如此缓慢的境地。

First I get my record ID s in PHP. (Lets assume $query returns an array of IDs)

表_products = Abunch of products

$query = "SELECT id,name FROM table_products WHERE name =  ".$name." ;";

Than I need to iterate through these records (NOTE): 可在另外两个表格中注明这些身份证的所在地点。

表_ 地点 = 配备一栋楼梯的桌子

链接-表 = 保持产品与地点之间的关系

$state = "somestate";

foreach($query as $row)
    {
    $query_two = "SELECT table_places.name, table_places.id, table_places.state, link_table.place_id, link_table.product_id 
    FROM table_places 
    INNER JOIN link_table 
    ON table_places.id = link_table.place_id 
    WHERE table_places.state =  ".$state."  AND link_table.product_id =  ".$row->id." ;";
    }

上帝希望这一点变得有意义。 我不问,如果我能够获得援助,使这一努力能够更快地运作,我将不胜感激。

最佳回答

痛苦在这里是:

foreach($query as $row)   <<--- you are pinging the DB to death.

回答两个问题:

SELECT 
  pl.name, pl.id, pl.state, 
  l.place_id, l.product_id,
  pr.name   
FROM table_places pl  
INNER JOIN link_table l ON (pl.id = l.place_id)  
INNER JOIN table_products pr ON (l.product_id = pr.id)
WHERE pr.name =  $name 
  AND pl.state =  $state 
ORDER BY pr.name, pl.state

确保对<代码>中所有使用领域的索引 http://www.un.org/Depts/DGACM/index_chinese.htm

问题回答

You can join more than two tables in one query. Untested query would be something like;

SELECT * FROM table_products AS prod LEFT JOIN (link_table AS link, table_places AS places)
    ON (prod.id=link.id AND prod.id=places.id)
WHERE some_field= some_value 

这肯定会提高你们的绩效,因为一个查询的频率比s/em>记录号码要快得多。 问询(因为你现在通过记录,每记录一次查询。)

The answer is simple: no control over database structure - no way to optimize.
As the indexing being the cornerstone of query optimization and you obviously need access to table structure to add one.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...