Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@odelalleau
Created October 25, 2012 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odelalleau/3952414 to your computer and use it in GitHub Desktop.
Save odelalleau/3952414 to your computer and use it in GitHub Desktop.
Queries explained
Related to: http://stackoverflow.com/questions/13056049/how-to-specify-the-from-tables-in-sqlalchemy-subqueries
1st Query:
EXPLAIN
SELECT MAX(d2.id)
FROM my_table d1
JOIN my_table d2
ON d2.id < d1.id
WHERE d1.id IN (111283470, 111283370, 111282470)
GROUP BY d1.id;
"id" "select_type" "table" "type" "possible_keys" "key" "key_len" "ref" "rows" "Extra"
"1" "SIMPLE" "d1" "range" "PRIMARY" "PRIMARY" "8" \N "3" "Using where; Using index; Using temporary; Using filesort"
"1" "SIMPLE" "d2" "index" "PRIMARY" "fk_severity" "1" \N "17832029" "Using where; Using index; Using join buffer"
2nd Query:
EXPLAIN
SELECT d2.id
FROM my_table d1, my_table d2
WHERE d2.id =
(SELECT d3.id FROM my_table d3 WHERE d3.id < d1.id ORDER BY d3.id DESC LIMIT 1)
AND d1.id IN (111283470, 111283370, 111282470);
"id" "select_type" "table" "type" "possible_keys" "key" "key_len" "ref" "rows" "Extra"
"1" "PRIMARY" "d1" "range" "PRIMARY" "PRIMARY" "8" \N "3" "Using where; Using index"
"1" "PRIMARY" "d2" "eq_ref" "PRIMARY" "PRIMARY" "8" "func" "1" "Using where; Using index"
"2" "DEP. SUBQ." "d3" "index" "PRIMARY" "PRIMARY" "8" \N "1" "Using where; Using index"
("DEP. SUBQ." stands for "DEPENDENT SUBQUERY", was abbreviated just to keep the table formatting cleaner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment