Question: I have a very simple nested query that is demanding 90+% CPU when it is called, and I can't seem to figure out why.

```
SELECT * FROM `push_log` 
WHERE push_id IN 
(SELECT `push_id` FROM push_sent_log 
    WHERE player_id='".$player_id."' 
        OR push_group='All' 
        AND `timestamp` >= DATE_SUB(CURDATE(), INTERVAL 24 hour) ) 
    ORDER BY timestamp DESC"

```

There are indexes on all the queried fields. Is there a more efficient way for me to do this?

Answer: 
Try remove subquery instead of `join`:

```
SELECT p.*
      FROM push_sent_log ps
      JOIN `push_log` p ON p.push_id= ps.push_id
    WHERE ps.player_id='".$player_id."' 
        OR ps.push_group='All' 
        AND `ps.timestamp` >= DATE_SUB(CURDATE(), INTERVAL 24 hour) ) 
    ORDER BY p.timestamp DESC"

```

also I think you need to check `where` in your sql,it seems doesn't make sense due to they are in the same level:

```
 WHERE ps.player_id='".$player_id."' 
            OR ps.push_group='All' 
            AND `ps.timestamp` >= DATE_SUB(CURDATE(), INTERVAL 24 hour)  -- why do you using OR and AND at the same time? 

```
Likely pushgroup , player\_id and push\_id of the table push\_sent\_log are not included in any index.
The EXPLAIN usually is required to answer this kind of questions.
Can you post What do you get from an explain applied to the nested SQL and to the full SQL?

<https://dev.mysql.com/doc/refman/5.7/en/using-explain.html>

In particular note the following statements from the page above:

EXPLAIN works with SELECT, DELETE, INSERT, REPLACE, and UPDATE statements.

When EXPLAIN is used with an explainable statement, MySQL displays information from the optimizer about the statement execution plan. That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. For information about using EXPLAIN to obtain execution plan information, see Section 8.8.2, “EXPLAIN Output Format”.

If you have a problem with indexes not being used when you believe that they should be, run ANALYZE TABLE to update table statistics, such as cardinality of keys, that can affect the choices the optimizer makes. See Section 13.7.2.1, “ANALYZE TABLE Syntax”.