Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: sql语句条件查询 效率低
Bug Report
I have a table with more than 720,000 rows of data. When using a query statement, if the WHERE condition field does not have an index, it will greatly reduce the query efficiency (200ms-800ms). Detailed information is as follows (already desensitized):
Table creation statement
DROP TABLE IF EXISTS `t_collect_record_1659571200`;
CREATE TABLE `t_collect_record_1659571200` (
`id` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,
`uid` varchar(64) NOT NULL ,
`src` int(8) NOT NULL ,
`collect_time` bigint(20) NOT NULL ,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `uid_ctime_id` (`uid`,`collect_time`,`id`) COMMENT 'uid_ctime_id'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin /*T![auto_rand_base] AUTO_RANDOM_BASE=780001 */;
Query statement
SELECT
`id`,
`src`,
`collect_time`
FROM
`t_collect_record_1659571200`
WHERE
`uid` = "11100000018"
AND `collect_time` <= 1660800000
AND `collect_time` >= 1659579000
AND IF (0=10,TRUE,id > 10)
AND IF (0=1,TRUE,src = 1) # Slow query if this condition is met
LIMIT
30;
Execution process comparison (modified time to avoid caching)
For formatting purposes, it has been uploaded to GitHub
【TiDB Version】
v5.4.2
【Impact of the Bug】
Increased query time
【Possible steps to reproduce the issue】
- Execute the table creation statement
- Insert about 700,000 rows of data
- Execute the query statement
【Observed unexpected behavior】 - When the second IF condition is met, the query time is 800ms; otherwise, it is only 200ms
【Expected behavior】 - Query time around 200ms
【Related components and specific versions】
【Other background information or screenshots】
If an environment is needed, you can contact QQ: 2029229189