Home > Database > Mysql Tutorial > MySQL索引之隔离列

MySQL索引之隔离列

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:57:56
Original
842 people have browsed it

如果在查询中没有隔离索引的列,mysql通常不会使用索引。隔离列意味着它不是表达式的一部分,也没有位于函数中。 例如: mysqlselect id from test where id +1 =5; 我们能轻易地看出where子句中的id等4,但是mysql却不会帮你求解方程,这取决于自己。我们应

如果在查询中没有隔离索引的列,mysql通常不会使用索引。“隔离“列意味着它不是表达式的一部分,也没有位于函数中。

例如:

mysql>select id from test where id +1 =5;

我们能轻易地看出where子句中的id等4,但是mysql却不会帮你求解方程,这取决于自己。我们应该养成简化where子句的习惯,这样就会把被索引的列单独放在比较运算符的一边。

再例如:

mysql>select ... where TO_DAYS(CURRENT_DATE) - TO_DAYS(date_col)
这个查询会查找date_cool值离今天不超过10天的所有行,但是它不会使用索引,因为使用了TO_DAYS()函数。

稍作修改:

mysql>select ... where data_cool >= DATE_SUB(CURRENT_DATE,INTERVAL 10 DAY);

这个查询就可以使用索引,但是它还可以改进。使用CURRENT_DATE将会阻止查询缓存把结果缓存起来,可以用常量替换掉CURRENT_DATE的值:

mysql>select ... where date_cool >= DATE_SUB('2012-08-29',INTERVAL 10 DAY);
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template