These are my links for May 13th through June 4th:
Tag: optimisation
A Little SQL “EXPLAIN” example
Background: I have a big table called “channel” with a few hundred thousand rows in – nothing vast, but big enough to cause some queries to run slower than I want.
Today I was fixing something else and happened to run
show full processlist;
I noticed this taking too long:
SELECT payload FROM channel LIMIT 398800,100;
This is a query used by some web-app paging code. Stupid really – payload isn’t indexed and there’s no use of any other keys in that query. Ok – how to improve it? First of all, see what EXPLAIN says:
mysql> explain SELECT payload FROM channel LIMIT 398800,100;
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | channel | ALL | NULL | NULL | NULL | NULL | 721303 | |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
Ok. So the simplest way would be to limit a selection of id_channel (the primary key) then select payloads in that set. First I tried this:
SELECT payload
FROM channel
WHERE id_channel IN (
SELECT id_channel FROM channel LIMIT 398800,100
);
Seems straightforward, right? No, not really.
ERROR 1235 (42000): This version of MySQL doesn't yet support
'LIMIT & IN/ALL/ANY/SOME subquery'
Next!
Second attempt, using a temporary table, selecting and saving the id_channels I’m interested in then using those in the actual query:
CREATE TEMPORARY TABLE channel_tmp(
id_channel BIGINT UNSIGNED NOT NULL PRIMARY KEY
) ENGINE=innodb;
INSERT INTO channel_tmp(id_channel)
SELECT id_channel
FROM channel LIMIT 398800,100;
SELECT payload
FROM channel
WHERE id_channel IN (
SELECT id_channel FROM channel_tmp
);
mysql> explain select id_channel from channel limit 398800,100;
+----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | channel | index | NULL | PRIMARY | 8 | NULL | 722583 | Using index |
+----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> explain select payload from channel where id_channel in (select id_channel from channel_tmp);
+----+--------------+-------------+-------------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | poss_keys | key | key_len | ref | rows | Extra |
+----+--------------+-------------+-------------+---------------+---------+---------+------+--------+-------------+
| 1 | PRIMARY | channel | ALL | NULL | NULL | NULL | NULL | 722327 | Using where |
| 2 | DEP SUBQUERY | channel_tmp | unique_subquery | PRIMARY | PRIMARY | 8 | func | 1 | Using index |
+----+--------------+-------------+-------------+---------------+---------+---------+------+--------+-------------+
2 rows in set (0.00 sec)
Let’s try a self-join doing all of the above without explicitly making a temporary table. Self-joins can be pretty powerful – neat in the right places..
mysql> explain SELECT payload
FROM channel c1,
(SELECT id_channel FROM channel limit 398800,100) c2
WHERE c1.id_channel=c2.id_channel;
+----+-------------+------------+--------+---------------+---------+---------+---------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+---------------+--------+-------------+
| 1 | PRIMARY | derived2 | ALL | NULL | NULL | NULL | NULL | 100 | |
| 1 | PRIMARY | c1 | eq_ref | PRIMARY | PRIMARY | 8 | c2.id_channel | 1 | |
| 2 | DERIVED | channel | index | NULL | PRIMARY | 8 | NULL | 721559 | Using index |
+----+-------------+------------+--------+---------------+---------+---------+---------------+--------+-------------+
3 rows in set (0.21 sec)
This pulls out the right rows and even works around the “no limit in subselect” unsupported mysql feature but that id_channel selection in c2 still isn’t quite doing the right thing – I don’t like all the rows being returned, even if they’re coming straight out of the primary key index.
A little bit of rudimentary benchmarking appears to suggest that the self-join is the fastest, followed by the original query at approximately one order of magnitude slower and trailing a long way behind at around another four-times slower than that, the temporary table. I’m not sure how or why the temporary table performance happens to be the slowest – perhaps down to storage access, or more likely my lack of understanding. Some time I might even try the in-memory table too for comparison.
Great pieces of code
A lot of what I do day-to-day is related to optimisation. Be it Perl code, SQL queries, Javascript or HTML there are usually at least a couple of cracking examples I find every week. On Friday I came across this:
SELECT cycle FROM goldcrest WHERE id_run = ?
This query is being used to find the number of the latest cycles (between 1 and 37 for each id_run) in a near-real-time tracking system and is used several times whenever a run report is viewed.
EXPLAIN SELECT cycle FROM goldcrest WHERE id_run = 231;
+----+-------------+-----------+------+---------------+---------+---------+-------+--------+-------------+
| id | select_type | table   | type | possible_keys | key   | key_len | ref  | rows  | Extra    |
+----+-------------+-----------+------+---------------+---------+---------+-------+--------+-------------+
|  1 | SIMPLE    | goldcrest | ref  | g_idrun    | g_idrun |    8 | const | 262792 | Using where |
+----+-------------+-----------+------+---------------+---------+---------+-------+--------+-------------+
In itself this would be fine but the goldcrest table in this instance contains several thousand rows for each id_run. So, for id_run, let’s say, 231 this query happens to return approximately 588,000 rows to determine that the latest cycle for run 231 is the number 34.
To clean this up we first try something like this:
SELECT MIN(cycle),MAX(cycle) FROM goldcrest WHERE id_run = ?
which still scans the 588000 rows (keyed on id_run incidentally) but doesn’t actually return them to the user, only one row containing both values we’re interested in. Fair enough, the CPU and disk access penalties are similar but the data transfer penalty is significantly improved.
Next I try adding an index against the id_run and cycle columns:
ALTER TABLE goldcrest ADD INDEX(id_run,cycle);
Query OK, 37589514 rows affected (23 min 6.17 sec)
Records: 37589514 Â Duplicates: 0 Â Warnings: 0
Now this of course takes a long time and, because the tuples are fairly redundant, creates a relatively inefficient index, also penalising future INSERTs. However, casually ignoring those facts, our query performance is now radically different:
EXPLAIN SELECT MIN(cycle),MAX(cycle) FROM goldcrest WHERE id_run = 231;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra             |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| Â 1 | SIMPLE Â Â Â | NULL Â | NULL | NULL Â Â Â Â Â | NULL | Â Â NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
SELECT MIN(cycle),MAX(cycle) FROM goldcrest WHERE id_run = 231;
+------------+------------+
| MIN(cycle) | MAX(cycle) |
+------------+------------+
| Â Â Â Â Â 1 | Â Â Â Â 37 |
+------------+------------+
1 row in set (0.01 sec)
That looks a lot better to me now!
Generally I try to steer clear of the mysterious internal workings of database engines, but with much greater frequency come across examples like this:
sub clone_type {
my ($self, $clone_type, $clone) = @_;
my %clone_type;
if($clone and $clone_type) {
$clone_type{$clone} = $clone_type;
return $clone_type{$clone};
}
return;
}
Thankfully this one’s pretty quick to figure out – they’re usually *much* more convoluted, but still.. Huh??
Pass in a clone_type scalar, create a local hash with the same name (Argh!), store the clone_type scalar in the hash keyed at position $clone, then return the same value we just stored.
I don’t get it… maybe a global hash or something else would make sense, but this works out the same:
sub clone_type {
my ($self, $clone_type, $clone) = @_;
if($clone and $clone_type) {
return $clone_type;
}
return;
}
and I’m still not sure why you’d want to do that if you have the values on the way in already.
Programmers really need to think around the problem, not just through it. Thinking through may result in functionality but thinking around results in both function and performance which means a whole lot more in my book, and incidentally, why it seems so hard to hire good programmers.