How to optimize this query (times out for semi-large tables)

I have a query that takes too long to finish (18 seconds on localhost, but it times out on a real server).

This is the table structure. The table has several thousand rows (will increase over time).

This is the query in question:

SELECT bef.Name, aft.Exp - bef.Exp AS Exp FROM ( 
SELECT * FROM people WHERE EntryID IN (SELECT Max(EntryID) FROM people GROUP BY Name) 
) AS aft
JOIN (
SELECT * FROM people WHERE EntryID IN (SELECT Min(EntryID) FROM people WHERE UpdateID >=1 GROUP BY Name) 
) AS bef
ON bef.Name = aft.Name
ORDER BY Exp DESC

It does this:

  • Find the earliest entry for each person with UpdateID >= 1
  • Find the most recent entry for each person.
  • Calculate the difference in Exp between the earliest entry, and the most recent entry for each person
  • Order that list by Exp, descending

Example:

Table

Result

EDIT: This is the plan it comes up with (idk how to interpret this).

Posted Jul 12, 2018, 12:13 PM UTC · edited

111
llameadrpc1•

Too many moving pieces to be optimized adequately. Break it out into different queries, each of which can be run optimally by the engine. Yes, multiple hits to the engine will be necessary. Beats the **** out of getting no response at all due to timeout.

3
MinecraftChrizzOP•

Do you have any advice on how to go about doing this? I'm pretty new to SQL. (This query alone took an hour of googling)

1
llameadrpc1•

What is your programming language of choice (PHP, Ruby, C#, Java, etc)? Each of those has a way of running a query and throwing the results into an array or object. Split your subqueries (everything inside those layers of nested parentheses) out into separate queries.

2
MinecraftChrizzOP•

I'm using PHP. This is the code I'm using:

$sql = "SELECT bef.Name, aft.Exp - bef.Exp AS Exp FROM ( 
SELECT * FROM people WHERE EntryID IN (SELECT Max(EntryID) FROM people GROUP BY Name) 
) AS aft
JOIN (
SELECT * FROM people WHERE EntryID IN (SELECT Min(EntryID) FROM people WHERE UpdateID >=1 GROUP BY Name) 
) AS bef
ON bef.Name = aft.Name
ORDER BY Exp DESC";
$result = mysqli_query($connection, $sql);

<table style="width:100%">
	<tr>
	<th>Rank</th>
	<th>IGN</th>
	<th>Exp</th>
	</tr>
	<?php 
	if(mysqli_num_rows($result) > 0) {
		$i = 1;
		while ($row = mysqli_fetch_assoc($result)) {
	?>
	<tr>
		<td><?php echo $i?></td>
		<td><?php echo $row['Name']?></td>
		<td><?php echo number_format ($row['Exp'])?></td>
	</tr>
	<?php 
		$i++;
		}
	}
	?>	  		  
</table> 

Which results in this table.

1
llameadrpc1•
2
OkieDaddy•

Here is a better way to write it, getting rid of the select *'s, and...don't quote me on it because its morning and no coffee, but I believe the join operations may be more effecient than the IN statements? YMMV. Edited for code cleaning. Reddit screwed up the formatting.

Edit 2: You'll also probably want a Nonclustered Index on (Name, EntryID) at minimum. If you want it covering, INCLUDE Exp...Not sure if INCLUDE is a keyword in MySQL, I've only done indexing in SQL Server.

SELECT
    [Base.Name],
    MaxEntry.Exp - MinEntry.Exp AS Exp
FROM
(
    SELECT 
        Name, 
        MAX(EntryID) MaxEntry, 
        MIN(EntryID) MinEntry
    FROM Person
    WHERE UpdateID >= 1
    GROUP BY Name
) AS Base
INNER JOIN Person MinEntry ON Base.Name = MinEntry.Name AND Base.MinEntry = MinEntry.EntryID
INNER JOIN Person MaxEntry ON Base.Name = MaxEntry.Name AND Base.MaxEntry = MaxEntry.EntryID
ORDER BY Exp DESC
2
MinecraftChrizzOP•

This works! And it's literally thousands of times faster than what I had! Thanks so much!

1
OkieDaddy•

Not a problem! Glad I could assist!

2
CrudBert•

I don’t think that you need either of those “order by” clauses. I’m not sure, but try taking those out, I think it’s just a lot of extra work.

1