How can I more efficiently perform a query that returns how many times an ID appears in two other tables?

Frogglet

I have found some solutions to this from other sources but none of them seem to be executing efficiently for me. I am using derby and my current solution is taking over a minute to execute!

I am trying to find the number of songs and albums that belong to a given artist and display those numbers in 2 separate columns next to the artist's name and id. ex:

ID    Name        Songs    Albums
425   J. Smith    0        0
314   A. Payne    32       3
412   K. Thomas   423      35

The artist table has artist_id, the song table has song_id and album_id, and the album table has album_id and artist_id. The tables aren't tiny. Artist has about 1,100 records, song has about 73,000, and album has about 7,000.

Here is my current solution:

select ar.artist_id, ar.artist_name, count(s.song_id), count(distinct(al.album_id))
from artist ar left outer join 
    (album al inner join song s 
    on al.album_id = s.album_id)
on ar.artist_id = al.artist_id 
group by ar.artist_id, ar.artist_name

Is there a way to make this query perform better? The ID columns are all primary keys in their respective tables, so they should already by indexed in derby if I understand correctly.

FuzzyTree

This query uses derived tables to get the song and album counts

select ar.artist_id, ar.artist_name, 
    coalesce(t1.song_cnt,0), coalesce(t2.album_cnt,0)
from artist ar left join (
    select artist_id, count(*) song_cnt
    from song group by artist_id
) t1 on t1.artist_id = ar.artist_id 
left join (
   select artist_id, count(*) album_cnt
   from album group by artist_id
) t2 on t2.artist_id = ar.artist_id

You might want to look into storing the counts themselves in your db and updating them using triggers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to perform a MySQL Query with 3 tables? One of the tables has foreign keys in the other two tables

From Dev

Count how many times a value appears in tables SQL

From Dev

How can I get a count of how many times a distinct value appears in a column?

From Dev

How to efficiently query many-to-many relationship on two columns

From Dev

Is it possible to combine 'DISTINCT' and 'COUNT' queries, so that I can see how many times each distinct value appears?

From Dev

How can I write this CSS more efficiently?

From Dev

How Can I Rewrite This More Efficiently

From Dev

How can I write this CSS more efficiently?

From Dev

How to perform join and updates from two or more tables using SSIS ?

From Dev

How can I update two different tables in one query?

From Dev

How can I query two tables using php PDO?

From Dev

How can I efficiently search for many strings at once in many files?

From Dev

How to perform an SQL query from a table based on values in other tables?

From Dev

How can I efficiently merge these two datasets?

From Dev

How can I make Javascript Countdown can perform two or more in one page?

From Dev

Django: How to perform correlated query from two tables?

From Dev

Get how many times an array appears in a List<>

From Dev

Count how many times an integer is appears in a column

From Dev

SQL How many times B appears in A

From Dev

Histogram of how many times a value appears in a dataset

From Dev

Count how many times a value appears in this array?

From Dev

How can I perform COALESCE in power query?

From Dev

How can I set up an alert that appears at all times?

From Dev

How can I get the number of times a substring appears in a text

From Dev

Count how many times a method can be called until it returns nil

From Java

How do I check for intersect of two many-to-many tables?

From Dev

MySQL: How do I efficiently reuse the results of a query in other queries?

From Dev

How can I model table to be shared by other tables with 0 to many relationship?

From Dev

How can I combine the $not Logical Query Operator in conjunction with other Comparison Query Operators to get a more specific yield?

Related Related

  1. 1

    How to perform a MySQL Query with 3 tables? One of the tables has foreign keys in the other two tables

  2. 2

    Count how many times a value appears in tables SQL

  3. 3

    How can I get a count of how many times a distinct value appears in a column?

  4. 4

    How to efficiently query many-to-many relationship on two columns

  5. 5

    Is it possible to combine 'DISTINCT' and 'COUNT' queries, so that I can see how many times each distinct value appears?

  6. 6

    How can I write this CSS more efficiently?

  7. 7

    How Can I Rewrite This More Efficiently

  8. 8

    How can I write this CSS more efficiently?

  9. 9

    How to perform join and updates from two or more tables using SSIS ?

  10. 10

    How can I update two different tables in one query?

  11. 11

    How can I query two tables using php PDO?

  12. 12

    How can I efficiently search for many strings at once in many files?

  13. 13

    How to perform an SQL query from a table based on values in other tables?

  14. 14

    How can I efficiently merge these two datasets?

  15. 15

    How can I make Javascript Countdown can perform two or more in one page?

  16. 16

    Django: How to perform correlated query from two tables?

  17. 17

    Get how many times an array appears in a List<>

  18. 18

    Count how many times an integer is appears in a column

  19. 19

    SQL How many times B appears in A

  20. 20

    Histogram of how many times a value appears in a dataset

  21. 21

    Count how many times a value appears in this array?

  22. 22

    How can I perform COALESCE in power query?

  23. 23

    How can I set up an alert that appears at all times?

  24. 24

    How can I get the number of times a substring appears in a text

  25. 25

    Count how many times a method can be called until it returns nil

  26. 26

    How do I check for intersect of two many-to-many tables?

  27. 27

    MySQL: How do I efficiently reuse the results of a query in other queries?

  28. 28

    How can I model table to be shared by other tables with 0 to many relationship?

  29. 29

    How can I combine the $not Logical Query Operator in conjunction with other Comparison Query Operators to get a more specific yield?

HotTag

Archive