Skip to content

Instantly share code, notes, and snippets.

@drsnyder
Created June 18, 2012 16:46
Show Gist options
  • Select an option

  • Save drsnyder/2949349 to your computer and use it in GitHub Desktop.

Select an option

Save drsnyder/2949349 to your computer and use it in GitHub Desktop.
All user albums
-- replacement
-- explain analyze @ 0.32ms
WITH a_user_albums AS (
SELECT *
FROM gallery_albums
WHERE
user_id = 1 AND
type != 'private' AND
category_id IN (SELECT id FROM user_gallery_album_category_visibility(0)) AND
(status & 1)::bool
),
non_selective AS (
SELECT *
FROM a_user_albums
WHERE
object_type IS NULL AND
type NOT IN ('default', 'community')
),
sets AS (
SELECT community.*
FROM a_user_albums
JOIN gallery_albums AS community ON (
a_user_albums.object_type = 62 AND
community.id = a_user_albums.object_id
)
WHERE (community.status & 1)::bool
)
SELECT *
FROM non_selective
UNION ALL
SELECT *
FROM a_user_albums
WHERE
type = 'default' AND
gallery_image_count > 0
UNION ALL
SELECT *
FROM sets
--- original
SELECT *
FROM "gallery_albums"
WHERE
"gallery_albums"."status" = 115 AND
"category_id" IN (SELECT id FROM user_gallery_album_category_visibility(0)) AND
"type" != 'private' AND
((object_type IS NULL AND type NOT IN ('default', 'community') AND user_id = 1)
OR (id IN (SELECT DISTINCT object_id FROM gallery_albums WHERE user_id = 1 AND object_type = 62 AND gallery_image_count > 0))
OR (id IN (SELECT DISTINCT id FROM gallery_albums WHERE user_id = 1 AND type = 'default' AND gallery_image_count > 0)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment