查询条件字段做运算优化
2011-12-27 14:31
316 查看
查询条件字段做运算优化
对字段做函数和运算完后无法使用索引。这么做的话,让优化器对字段产生歧义,不知道如何选择索引。因为这时候并没有一个concat(year_id,'-',month_id)这样的字段mysql> explain select * from data_market.mkt_order_by_month_userid_type where concat(year_id,'-',if(month_id<10,'0',''),month_id)='2011-09'; +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ | 1 | SIMPLE | mkt_order_by_month_userid_type | ALL | NULL | NULL | NULL | NULL | 3673176 | Using where | +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from data_market.mkt_order_by_month_userid_type where concat(year_id,'-',month_id)='2011-09'; +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ | 1 | SIMPLE | mkt_order_by_month_userid_type | ALL | NULL | NULL | NULL | NULL | 3673176 | Using where | +----+-------------+--------------------------------+------+---------------+------+---------+------+---------+-------------+ 1 row in set (0.00 sec)
优化方法,对要传入的值做函数操作
mysql> explain select * from data_market.mkt_order_by_month_userid_type where year_id=substring('2011-09',1,4) and month_id = substring('2011-09',6,2); +----+-------------+--------------------------------+------+---------------+---------+---------+-------------+--------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------------------------+------+---------------+---------+---------+-------------+--------+-------+ | 1 | SIMPLE | mkt_order_by_month_userid_type | ref | year_id | year_id | 8 | const,const | 377128 | | +----+-------------+--------------------------------+------+---------------+---------+---------+-------------+--------+-------+
相关文章推荐
- 万能分页存储过程(带第一页性能优化,任意条件查询,任意字段排序)
- MySQL 查询优化(查询条件字段传值要和字段取值类型一致)
- MySQL数据库优化总结如果索引多个字段,第一个字段要是经常作为查询条件的。如果只有第二个字段作为查询条件,这个索引不会起到作用;
- 针对where条件中日期型字段前面加函数的慢查询的优化
- 针对where条件中日期型字段前面加函数的慢查询的优化
- 注意使用 BTREE 复合索引各字段的 ASC/DESC 以优化 order by 查询效率
- SQL语句优化写法心得 - 条件查询(咋个办呢 zgbn)
- 用Date字段作为where条件查询oracle数据库
- Oracle大字段(clob)模糊查询优化方法
- 使用正则表达式获取Sql查询语句各项(表名、字段、条件、排序)
- sql语句查询,多字段like模糊查询优化
- 注意使用 BTREE 复合索引各字段的 ASC/DESC 以优化 order by 查询效率
- 一个字段同时满足多个条件的查询
- SQL优化一例:查询条件内移,减少HASH JOIN代价 推荐
- SQL一次性查询一个字段不同条件下的统计结果
- 无合适where条件过滤时尽量选择order by后的字段以驱动表进行查询 推荐
- mysql中查询条件字段为空的查询
- Sqlite 数据库插入标示字段 获取新Id 及利用索引优化查询
- ABAP--在查询条件只包含部分索引字段时,如何使用索引
- mysql: 两个字段合并,字符时间转时间戳,别名字段作为where条件查询