Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL optimization distinct

Suppose I have an SQL query using Oracle SQo Server to get the data from the database as below

select  distinct ORDER_TYPE_name as ORDER_TYPE from 
PRODUCT_MASTER_LIST where PROJECT_ID = 99999
order by  ORDER_TYPE ASC 

I now have 5000 records with the following order Types:

Red
Yellow
Green
Black 
null
Unclassified

How to optimise the performance by shortening the query execution time?

Note When I see the execution plan, there are many full access through the table?

like image 475
Raju yourPepe Avatar asked Mar 08 '26 11:03

Raju yourPepe


2 Answers

You can define an index on those two columns to prevent table scans. That should bring down the execution time by a significant extent.

CREATE INDEX IX_ProductMasterList_OrderType 
ON PRODUCT_MASTER_LIST(PROJECT_ID, ORDER_TYPE);
like image 196
Vikdor Avatar answered Mar 10 '26 03:03

Vikdor


I think index on PROJECT_ID could be the right solution. It depends on selectivity of this column.

CREATE INDEX PRODUCT_ML_PRODUCT_ID_IDX ON PRODUCT_MASTER_LIST(PRODUCT_ID);
like image 42
Petr Pribyl Avatar answered Mar 10 '26 02:03

Petr Pribyl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!