search

Home  >  Q&A  >  body text

c# - mysql query optimization?

1. There are more than 1 million pieces of data. The count speed is extremely slow. Please let me know how to optimize it.

2. Code:

SELECT
    COUNT(*)
FROM
    `score`
INNER JOIN `users` ON (
    `score`.`UID` = `users`.`UID`
)
WHERE
    (`score`.`Score` >= 10)
AND (`score`.`Score` <= 81);

3, Index

user table

怪我咯怪我咯2745 days ago725

reply all(5)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:53:56

    count(id) not count(*)

    reply
    0
  • 黄舟

    黄舟2017-05-18 10:53:56

    You are joining a table from the left. It is recommended to add an index to the UID

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-18 10:53:56

    Why do we need inline tables for statistics? If we want to group, wouldn’t it be better to use group by UID?

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:53:56

    The statement is simple, the index is normal, and it should not be slow. You can post the execution plan through explain + statement.
    In addition, the users table structure is also posted. If UID is the primary key of the users table, you can remove the connection of the table

    reply
    0
  • 为情所困

    为情所困2017-05-18 10:53:56

    I don’t see the necessity of connecting tables. You only need to count the number of data in fractional segments. I don’t see that it has anything to do with the user table. Just count in a single table.

    reply
    0
  • Cancelreply