Select top 10 authors with most articles published in MySQL really slow

Eric Young

I try to make a list of users having the most articles published (or something like that).

So I wrote something like this:

SELECT u.id, COUNT(a.id) AS articles
FROM users u
LEFT JOIN articles a ON a.user_id = u.id
GROUP BY u.id
ORDER BY articles DESC
LIMIT 10

The 'explain' command shows it 'Using join buffer (Block Nested Loop)' and 'Using temporary; Using filesort'.

It costs really long time to get the result. I know the reason that causes this problem. But I don't know how to solve it.

Also, I wanted to sort records by user's register time instead of published articles as an option.

And I know I can add a column to users table accumulates the articles number, but I'm not be able to change the original program.

It seems to me that this is the only way to make the list. But also the impossible way to retrieve the result before my time.

Is there another faster way to get a top 10 list based on join queries?

Any suggestion would be appreciated.

tatskie

Try this:

SELECT
    u.id, t.articles
FROM USERS u
LEFT JOIN
    (
    SELECT user_id, COUNT(id) AS `articles`
    FROM articles
    GROUP BY user_id
    ) t ON u.id = t.user_id
ORDER BY t.articles DESC
LIMIT 10

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to scrape the links, titles, authors and timestamps for the 10 articles here? Soup output looks different from browser

分類Dev

How to get top 10 customers with most turnover?

分類Dev

Select top score only from MySQL database

分類Dev

pandas groupby apply is really slow

分類Dev

MySQL select with multiple many to many joins causing very slow query

分類Dev

Oracle SELECT TOP10レコード

分類Dev

unable to select top 10 records per group in sparksql

分類Dev

mysql select most recent row by date for each user

分類Dev

JS: writing a function that iterates through a list of strings and returns the top 10 most frequent strings in the list

分類Dev

SQL: How to display the top 10 list of the users name who has the most item?

分類Dev

MySQL for counting how many top 10 scores a user has

分類Dev

AWS RDS SQL Server failover really slow

分類Dev

Select first largest value using TOP 1 in MySQL?

分類Dev

Slow MongoDB query on top of index

分類Dev

SQLite Slow Select Query

分類Dev

Slow select - PostgreSQL

分類Dev

Slow mysql performance

分類Dev

MySQL query becomes slow

分類Dev

Is Kivy widgets creation really so slow or am I doing it wrong?

分類Dev

I keep getting certificate errors and websites not loading correctly or really slow

分類Dev

Do most compilers transform % 2 into bit comparison? Is it really faster?

分類Dev

TOP(10)に関するmysql_fetch_array()の問題

分類Dev

Top 3 most common values using groupby

分類Dev

What is a Top-most class in Java?

分類Dev

Django select 2 Multiple Select Slow Load

分類Dev

SELECT the most occuring value PostgreSQL

分類Dev

Executing extremely slow MySQL query

分類Dev

Slow MySQL Inserts from Python

分類Dev

Mysql Query running very slow

Related 関連記事

  1. 1

    How to scrape the links, titles, authors and timestamps for the 10 articles here? Soup output looks different from browser

  2. 2

    How to get top 10 customers with most turnover?

  3. 3

    Select top score only from MySQL database

  4. 4

    pandas groupby apply is really slow

  5. 5

    MySQL select with multiple many to many joins causing very slow query

  6. 6

    Oracle SELECT TOP10レコード

  7. 7

    unable to select top 10 records per group in sparksql

  8. 8

    mysql select most recent row by date for each user

  9. 9

    JS: writing a function that iterates through a list of strings and returns the top 10 most frequent strings in the list

  10. 10

    SQL: How to display the top 10 list of the users name who has the most item?

  11. 11

    MySQL for counting how many top 10 scores a user has

  12. 12

    AWS RDS SQL Server failover really slow

  13. 13

    Select first largest value using TOP 1 in MySQL?

  14. 14

    Slow MongoDB query on top of index

  15. 15

    SQLite Slow Select Query

  16. 16

    Slow select - PostgreSQL

  17. 17

    Slow mysql performance

  18. 18

    MySQL query becomes slow

  19. 19

    Is Kivy widgets creation really so slow or am I doing it wrong?

  20. 20

    I keep getting certificate errors and websites not loading correctly or really slow

  21. 21

    Do most compilers transform % 2 into bit comparison? Is it really faster?

  22. 22

    TOP(10)に関するmysql_fetch_array()の問題

  23. 23

    Top 3 most common values using groupby

  24. 24

    What is a Top-most class in Java?

  25. 25

    Django select 2 Multiple Select Slow Load

  26. 26

    SELECT the most occuring value PostgreSQL

  27. 27

    Executing extremely slow MySQL query

  28. 28

    Slow MySQL Inserts from Python

  29. 29

    Mysql Query running very slow

ホットタグ

アーカイブ