CRITICAL
Rule Definition
Identify "DISTINCT", "DISTINCTROW", "UNIQUE" modifiers in SQL "SELECT" statements:
- is frequently a "code smell"
- it indicates that something is not quite right because using the DISTINCT keyword means that redundant data is being pulled from the database and then discarded
- applicable for all sql, including embedded sql
Remediation
Check the statement and if so, rearrange the "WHERE" or the "FROM" clause in the SQL "SELECT" statement to only get the rows you need.
Violation Code Sample
The following SQL "SELECT" statement should be reviewed:
SELECT DISTINCT u.user_id, u.user_name, u.realname, u.email, u.confirm_hash
FROM users u, user_group ug, groups g
WHERE u.status='A'
AND u.user_id=ug.user_id
AND ug.admin_flags='A'
AND g.status='A'
AND g.group_id=ug.group_id
ORDER BY u.user_id;
Fixed Code Sample
Add an uncorrelated subquery:
SELECT u.user_id, u.user_name, u.realname, u.email, u.confirm_hash
FROM users u
WHERE u.status = 'A'
AND u.user_id
IN (
SELECT ug.user_id
FROM user_group ug, groups g
WHERE ug.admin_flags='A'
AND g.status = 'A'
AND g.group_id = ug.group_id
)
ORDER BY u.user_id;
Related Technologies
Technical Criterion
Efficiency - SQL and Data Handling Performance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.