How does the SQL Optimizer handle Select Distinct on a primary key? For eg
Employee Table – (EmpId int primary key, EmpName)
select distinct EmpId from Employee.
Would the optimizer reject the distinct keyword considering EmpId is already distinct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Tried under Oracle 11g and they produce different results.
Produces:
And:
Produces:
USERIDis the primary key onTPM_USERin case that wasn’t clear.UPDATE:
I was somewhat irked that Oracle could be this stupid, so I tried the same queries on our production server which has about a thousand times more data. This time, the two query plans were identical (the index was used both times, and no full table scan was done). This leads me to believe the planner will take the table statistics into account when deciding which route to go.
Production (with or without
DISTINCT):