Skip to content

Instantly share code, notes, and snippets.

@jon-zawada
Created April 9, 2020 04:59
Show Gist options
  • Select an option

  • Save jon-zawada/4fbea4c94f93032c8550167e18cf4fa6 to your computer and use it in GitHub Desktop.

Select an option

Save jon-zawada/4fbea4c94f93032c8550167e18cf4fa6 to your computer and use it in GitHub Desktop.
This query will give me the three related songs per song genre to display in my related songs component
nimbus=# SELECT * FROM songs JOIN users ON songs.id_user = users.id LIMIT 3;
id | song_name | song_plays | song_comments | song_image_url | song_genre | id_playlist | id_user | id | user_name | user_imageurl | user_location | user_follower_count
----+--------------------------+------------+---------------+--------------------------------------+------------+-------------+---------+---------+-------------------+---------------------------------------------------------------------+--------------------+---------------------
1 | ab voluptatem et | 88405 | 84363 | http://lorempixel.com/640/480/cats | edm | 125840 | 997611 | 997611 | Mrs. Vicente Hand | https://s3.amazonaws.com/uifaces/faces/twitter/brandonburke/128.jpg | Majorburgh | 55671
2 | velit accusantium labore | 85421 | 83485 | http://lorempixel.com/640/480/cats | country | 248725 | 7734294 | 7734294 | Adrianna Dietrich | https://s3.amazonaws.com/uifaces/faces/twitter/sdidonato/128.jpg | North Gisselletown | 92846
3 | qui neque veniam | 20481 | 73833 | http://lorempixel.com/640/480/nature | edm | 460079 | 9040482 | 9040482 | April Haley | https://s3.amazonaws.com/uifaces/faces/twitter/denisepires/128.jpg | Walkerton | 27442
(3 rows)
nimbus=# EXPLAIN ANALYZE SELECT * FROM songs JOIN users ON songs.id_user = users.id LIMIT 3;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.43..2.10 rows=3 width=184) (actual time=0.061..0.077 rows=3 loops=1)
-> Nested Loop (cost=0.43..5539146.51 rows=10000064 width=184) (actual time=0.060..0.075 rows=3 loops=1)
-> Seq Scan on songs (cost=0.00..244653.64 rows=10000064 width=83) (actual time=0.037..0.037 rows=3 loops=1)
-> Index Scan using users_pkey on users (cost=0.43..0.53 rows=1 width=101) (actual time=0.009..0.009 rows=1 loops=3)
Index Cond: (id = songs.id_user)
Planning Time: 2.505 ms
Execution Time: 1.405 ms
(7 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment