mysql: query taking forever with no result
This query is against a MyISAM table:
SELECT * FROM table
WHERE link_id = 1 and counted = 1
Now, if I don't pass counted, it gets it real开发者_JS百科ly fast. When I pass counted to it, it just takes forever. It is a big table, but I don't see the reason why it should get stuck instead of just showing me an empty table.
Any ideas?
Run this to see if you're table has proper indexes. Perhaps counted does not have an index?
EXPLAIN SELECT * FROM table WHERE link_id = 1 and counted = 1
An index on (link_id, counted) will make that query fast.
It's because it has to filter counted=1
through each and every row. Do you have an index set up on link_id
? If so, you can set one up for counted
My guess is that you are simply missing an index. Try this:
SHOW INDEXES FROM <your table name>
Do you see counted
in the column_name
field?
If not, then you probably want to:
CREATE INDEX <nifty index name> ON <your table name> (counted);
I would next look to EXPLAIN
(which Micah explains here). (I only suggest explain second because my experience is that the majority of the time the problem is simply a missing index if a query is slow.)
精彩评论