Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql查询速度的差异_Php_Mysql_Sql - Fatal编程技术网

Php mysql查询速度的差异

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

假设我的数据库中有1kk记录

现在我需要选择一些数据,我还需要知道我选择了多少字段,所以我的问题是:

运行一个查询来计算以下数据是否更好:

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];

不,如果您已经运行了一个查询来获取一组数据,则无需运行另一个查询来获取计数。这些信息已经创建,您可以通过多种方式获取。不,如果您已经运行一个查询来获取一组数据,则无需运行另一个查询来获取计数。这些信息已经被创建,您可以通过多种方式获取。