'Mysql) when i using user-defined variable , SQL explain use type "ALL"

When i using user-defined variable , i want to use 'index' like 'ref..';

for example,

SET @company_code = "A002";

select *
from product_in_out 
where  company_code = @company_code
  and product_date = "2022-04-13"
  and out_type = "Q"
;

above result like this, [enter image description here][1]

for using SQL index, i've tested that variable to plain text like "A002".

at that time, SQL use index like 'ref'

select *
from product_in_out
where  company_code = "A002"
  and product_date = "2022-04-13"
  and out_type = "Q"
;

if so, is there any good way to use valiable for using mysql Index?!?!



Solution 1:[1]

As a workaround, add this composite index:

INDEX(out_type, product_date, company_code)

(I assume product_date is of datatype DATE.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick James