Optimize query with composite index

94 views Asked by At

"I'm having trouble with a query that uses a composite index. When I use a regular index, the query takes about 1 second to execute on a table with 3 million rows. However, when I add a composite index, the query only improves by about 0.982 seconds or 0.88 seconds, which isn't a significant improvement. The composite index I created is CREATE OR REPLACE INDEX idx ON bill_lading (c, k, p, cli). However, when I run EXPLAIN on the query, it only shows that the c_date index is being used, not the composite index. Can you help me understand how to use the composite index effectively?"

SELECT
`id`,
`cav`,
`cave`,

FROM
`b` 
WHERE 
`c` = '0' 
AND `k` = '1' 
AND `p` = '0' 
AND `cli` IN ( '1', '2', '3', '4', '5', '11' ) 
AND `c_date` <= '2021-02-15 23:59:59' 
AND ((
    `cav` <> '0' 
    AND ( `c_check_id` <> '0' OR `number_r` = '1' ) 
    AND `status` = '3' 
    ) 
  OR (
    `cav` = '0' 
  AND `status` IN ( '1', '2' ))) 
ORDER BY
`id` ASC 
LIMIT 200 OFFSET 400;

NOTE: all column c, k, p, cli,c_date, c_check_id, status have single index in table b

0

There are 0 answers