Scholli
Scholli

Reputation: 429

Why MySQL query is so slow with two WHEREs

i have a problem. Within the time, i get much rows in mysql.

To Filter i want to get some informations.

This is my query

SELECT COUNT(*) AS cases,
       SUM(`item_price`) AS preturile
  FROM cases
  WHERE opened = 1 AND
        trade_id = '1234'

It is very slow... needs 1.5 secs or something

If i kick out opened = 1, so it looks like that

SELECT COUNT(*) AS cases,
       SUM(`item_price`) AS preturile
  FROM cases
  WHERE trade_id = '1234'

The speed is fast and good! But i need that opened 1 in there... But why is that so slow?

opened is int(11) and has INDEX.

I dont know what i can do there, its so slow...

Upvotes: 1

Views: 53

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

be sure you have a proper index eg a composite index on the columns trade_id, opened

create index  myidx on cases ( trade_id, opened )

Upvotes: 2

Related Questions