PHP中分页肯定会使用到MySQL的limit,大部分对类似”select * from title where uid =** order by id desc limit m,n”很熟悉,也不是全部都能看出里面有什么不对,可是当是在大数据量下操作呢,比如百万类似”select * from title where uid =177 order by id desc limit 1234567,20″就会发现sql执行的时间明显变得很长,为什么呢?
先从MySQL的limit原理说起,使用limit m,n是时候,MySQL先扫描(m+n)条记录,然后从m行开始取n行.比如上面的例子就是先扫描1234587条数据,这样的话sql能快吗?这就要 求我们尽可能的减少m的值,甚至没有m直接limit n这样是sql.
看个例子:
mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 limit 888888,10;
+———-+—————————–+———————+——+———————-+
| id | substr(mobile from 1 for 7) | time | cpid | linkid |
+———-+—————————–+———————+——+———————-+
| 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
| 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
| 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
| 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
| 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
| 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
| 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
| 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
| 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
| 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
+———-+—————————–+———————+——+———————-+
10 rows in set (3.84 sec)
mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 and id>=11535090 limit 10;
+———-+—————————–+———————+——+———————-+
| id | substr(mobile from 1 for 7) | time | cpid | linkid |
+———-+—————————–+———————+——+———————-+
| 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
| 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
| 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
| 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
| 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
| 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
| 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
| 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
| 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
| 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
+———-+—————————–+———————+——+———————-+
10 rows in set (0.00 sec)
mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 and time>=’2010-02-24 21:07:48′ limit 10;
+———-+—————————–+———————+——+———————-+
| id | substr(mobile from 1 for 7) | time | cpid | linkid |
+———-+—————————–+———————+——+———————-+
| 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
| 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
| 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
| 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
| 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
| 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
| 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
| 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
| 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
| 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
+———-+—————————–+———————+——+———————-+
10 rows in set (0.01 sec)
例中数据表id是主键,time也建了索引,表中总数据约为240w行,其中cpid为769的数据量大约为90w条.这里面的id和时间可能会是不连续的.故不能直接得获取id>m这样操作
所以可以显示 “1,2,3,4,5,末页” 或是 “首页,<<100,101,102,103 >>末页”这样,这样可以极大的减少m值!
MySQL里面的join顺便说一句就是,通常有点讲究的是用小表去驱动大表,而由于MySQL join实现的原理就是做循环比如left join就是对左边的数据进行循环去驱动右边的表,比如左边是可能会有m条记录匹配,右边有n条记录那么就是做m次循环,每次扫描n行数据,总扫面行数是 m*n行数据.左边返回的结果集的大小就决定了循环的次数,故单纯的用小表去驱动大表不一定的正确的,小表的结果集可能也大于大表的结果集,所以写 join的时候尽可能的先估计两张表的可能结果集,用小结果集去驱动大结果集.值得注意的是在使用left/right join的时候,从表的条件应写在on之后,主表应写在where之后.否则MySQL会当作普通的连表查询!
SQL 外链接操作小结 inner join left join right join
延伸阅读
- MYSQl联合查询效率分析:left join
- 实例解析:mysql left join,right join,inner join用法分析(不同点区别)
- SQL LEFT JOIN 关键字
- mysql多表join联查语句错误:#1241 - Operand should contain 1 column(s)
- MySQL联表查询详解/超详细mysql left join,right join,inner join用法分析比较
- SQL 外链接操作小结 inner join left join right join
- MySQL联表查询优化:LEFT JOIN 与 RIGHT JOIN
- MySQL鲜为人知的几个特殊技巧
- 手把手教你在centos下编译安装配置高性能web服务器环境nginx+php(fast-cgi)+mysql等/适合小内存vps使用
- mysql错误/usr/libexec/mysqld: Out of memory (Needed xxx bytes)及The table 'pre_common_session' is full处理手记
- 全球IP地址分配表_MySQL版_by_fengyqf100131_ip.sql.zip
- DB2MYSQL:最好用的ms access转换为mysql工具/开源免费/access 导入mysql
- 一次linux下mysql服务优化(环境为vps上的centos5)
- 总结MYSQL的优化
- 远程登入MySQL出错问题原因分析及解决办法/无法远程登入MySQL数据库
0 条评论。