select语句where的作用
发布于 2007-09-14 20:52 7 阅读:41,197 评论:7 标签: mysql select where

      where,是个好东西!

      但你面对千万级的数据,如何更快的所用遍历数据,并根据自己的需要处理数据?where会起到多大的作用?

      当然,前提是数据中的某数据有它的规律。

      假如,有一表有一个字段tableid为int,唯一且索引。在这个时候我们可以使用where字句让遍历加速。

      比较以下两个语句:

以下是代码片段:

select * from `tablename` order by `tableid` limit $numStart, 10000;

select * from `tablename` where `tableid`>'$tableidStart' order by `tableid` limit 10000;

      上面的例子是想根据tableid的顺序来遍历整个表,所以有个排序的字句。显然,对于千万级别的库或表来说排序这个费时间的事情,但是没关系,前面已经假设它有索引了,所以时间相对还是可以承受的。

      索引有这么大的作用么?不信可以看看mysql数据存储目录下的.MYI文件的大小,再看看.MYD文件的大小,一目了然!常言道,不能占着茅坑不拉屎,mysql很好的避免了这一点!

      相信你可以注意到limit子句中,偶限制的条件是“10000”。在损耗内存和操作数据库的权衡中,我还是比较倾向损耗内存。

      言归正传,说一下where子句。如果没有where,只有“$numStart, 10000”,那么这个查询会如何进行呢?mysql会根据“order by `tableid`”以后,再找到前$numStart条数据,然后从第$numStart+1条数据开始返回1000条数据。请注意这个“再找到前$numStart条数据”的时间,它是随着$numStart的增加而增加的!

      时间的损耗,正在于此。没有时间的教训是不会注意到这个的。我曾犯过这个错误,然后认识到了这一点。但是,时间一长......又忘记啦!

      如果在这个时候,我们加上一个“where `tableid`>'$tableidStart'”,时间的损耗立马减少了!这个字段是有索引的,找到一个特定的值的时间几乎可以忽略!在这个时候,对这个有索引的字段排序再找前10000条(“limit 10000”),简直易如反掌!

      在我的测试中,假设没有where子句的语句要跑5个小时,那么加上这个where子句,只需40分钟!这个时间是怎么节省出来的?主要是在“再找到前$numStart条数据”的时间,前面提到这个几乎是个等差数列的方式的增长。而加上where子句,这个时间差几乎为0!

      特以此文,警示自己不再忘记!

      两条语句中使用两个变量,变量怎么来的,大家根据情况而定了。

PHP写也应该是很简单的事情

我看了你的代码学到了一些东西根思想哈哈

by ilsanbao 2007-10-01 18:55:33
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
命令行都可以

命令行下也可以显示执行时间

但如果要在PHP程序中显示,估计就要自己去做这个事情了

by yayu 2007-09-26 16:32:56
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
PHPMYADMIN也可以吧

上面直接显示SQL语句执行时间的

by ilsanbao 2007-09-25 09:05:31
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
to mdy

没看见PHP有这么的函数

在语句执行的时候,用PHP取得前后时间,再取差也是可以的嘛

by 芽雨 2007-09-24 10:31:10
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
WHERE很棒

今天没事无聊我也测试了下!果然快不少 呢

by ilsanbao 2007-09-21 13:26:49
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
学习

怎么察看一个查询索要运行的时间

你的留言怎么搞得。高级机器人?搞笑

by mdy 2007-09-19 16:54:09
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
好 学习

你做测试用了半天呀 !辛苦

by ilsanbao 2007-09-18 09:17:41
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172