Php mysql查询速度的差异
假设我的数据库中有1kk记录 现在我需要选择一些数据,我还需要知道我选择了多少字段,所以我的问题是: 运行一个查询来计算以下数据是否更好:Php mysql查询速度的差异,php,mysql,sql,Php,Mysql,Sql,假设我的数据库中有1kk记录 现在我需要选择一些数据,我还需要知道我选择了多少字段,所以我的问题是: 运行一个查询来计算以下数据是否更好: SELECT COUNT("id") from table where something = 'something' SELECT 'some_field' from table where something = 'something'; 然后再运行一个查询,以进行如下选择: SELECT COUNT("id") from table where s
SELECT COUNT("id") from table where something = 'something'
SELECT 'some_field' from table where something = 'something';
然后再运行一个查询,以进行如下选择:
SELECT COUNT("id") from table where something = 'something'
SELECT 'some_field' from table where something = 'something';
或也许最好只选择数据,然后用php计算数据,如:
count($rows);
或者可能有更好的方法,例如在一个查询中完成所有操作?如果您获取了所有1000条记录,那么您可以在获取时进行计数:
$res=mysql_query("SELECT 'some_field' from table where something = 'something'");
while($r = mysql_fetch_*($res)) {
$count++;
//> Do stuff
}
这样,您只进行一次查询,而不使用
mysql_num_rows()代码>如果您获取了所有1000条记录,那么您可以在获取时进行计数:
$res=mysql_query("SELECT 'some_field' from table where something = 'something'");
while($r = mysql_fetch_*($res)) {
$count++;
//> Do stuff
}
这样,您只进行一次查询,而不使用mysql_num_rows()代码>一个查询是:
SELECT Count(*) AS NumRows, some_field from table
GROUP BY some_field where something = 'something';
一个问题是:
SELECT Count(*) AS NumRows, some_field from table
GROUP BY some_field where something = 'something';
从字里行间看,我想你可能想要的是。这允许您选择结果集的一部分(使用LIMIT
子句),并且仍然可以在单个操作中计算匹配行的总数。您仍然使用两个查询,但数据中的实际搜索操作只发生一次:
// First get the results you want...
$result = mysql_query("
SELECT SQL_CALC_FOUND_ROWS
FROM `table`
WHERE `something` = 'something'
LIMIT 0, 10
");
// ...now get the total number of results
$numRows = mysql_query("
SELECT FOUND_ROWS()
");
$numRows = mysql_fetch_row($numRows);
$numRows = $numRows[0];
从字里行间看,我想你可能想要的是。这允许您选择结果集的一部分(使用LIMIT
子句),并且仍然可以在单个操作中计算匹配行的总数。您仍然使用两个查询,但数据中的实际搜索操作只发生一次:
// First get the results you want...
$result = mysql_query("
SELECT SQL_CALC_FOUND_ROWS
FROM `table`
WHERE `something` = 'something'
LIMIT 0, 10
");
// ...now get the total number of results
$numRows = mysql_query("
SELECT FOUND_ROWS()
");
$numRows = mysql_fetch_row($numRows);
$numRows = $numRows[0];
不,如果您已经运行了一个查询来获取一组数据,则无需运行另一个查询来获取计数。这些信息已经创建,您可以通过多种方式获取。不,如果您已经运行一个查询来获取一组数据,则无需运行另一个查询来获取计数。这些信息已经被创建,您可以通过多种方式获取。