Skip to content
Advertisement

My data fetching too slow. Is there anything wrong with my sql?

I’m current facing a issue in my project. When i search a product, it take too much time to fetch data. This is sql code.

SELECT * FROM product_info 
    LEFT JOIN (
        SELECT bulk_stock_info.bulk_unit_buy_price,bulk_stock_info.general_unit_sale_price,bulk_stock_info.bulk_unit_sale_price,bulk_stock_info.product_id as pid 
        FROM bulk_stock_info 
        ORDER BY bulk_id DESC
        ) bulk_stock_info 
    ON bulk_stock_info.pid = product_info.product_id 
WHERE (`product_name` RLIKE ' +$query') OR `product_name` LIKE '$query%';"

Advertisement

Answer

There is no reason to use a subquery or outer join:

SELECT pi.*
       bsi.bulk_unit_buy_price, bsi.general_unit_sale_price, bsi.bulk_unit_sale_price
FROM product_info pi JOIN
     bulk_stock_info bsi
     ON bsi.product_id = pi.product_id 
WHERE pi.product_name RLIKE ' +$query' OR pi.product_name LIKE '$query%'
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement