Posts Tagged ‘MySQL’

mysqlslap and SQL syntax error

September 23rd, 2009

While I was playing with mysqlslap for my local benchmarks I got an error that puzzled me :

You have an error in your SQL syntax; ...

After analysing the query and running it with the CLI mysql client and in other tools I tried to write the query on one line.

That was the solution, it seems mysqlslap only accepts one line queries :/

I tried with the following version

mysqlslap  Ver 1.0 Distrib 5.1.39, for pc-linux-gnu (i686)

‘Hope that helps

:)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

MySQL and SELECT, the evil ORDER BY clause

August 13th, 2009

I was at a customer, helping them to make their import/export run fast and after testing a few things we realized that only one query was the source of the problem.

The query looked like this :

SELECT id, [...]
FROM the_table
WHERE  the clause
ORDER BY id ASC;

After a few tests we discovered that this query took around 15 seconds to run on only around 1 000 000 rows.

Running explain show the following result (slightly modified for more readability)

+----+------+-----+------------+--------+-------+-----+--------+-----------+
| id | [...] | type | possible_keys | key | key_len | ref | rows | Extra |
+----+------+-----+------------+--------+-------+-----+--------+-----------+
| 1 | [...] | index | idx1, idx2 | PRIMARY | 8 | NULL | 1103921 | Using where |
+----+------+-----+------------+--------+-------+-----+--------+-----------+

The problem is that they already were the correct indices on this table but MySQL was not able to choose the correct one. I tried to enforce the use of the correct INDEX with USE INDEX( … ) in the query and it worked fine but I did not like this solution.

After that I noticed that the ORDER BY clause was useless since the field used to sort was the primari key id.
And even if this clause is removed, the result set will be sorted on the field ‘id’.

You can verify this with the simple SQL below :


CREATE TABLE table1 (
id tinyint(4) NOT NULL AUTO_INCREMENT,
field varchar(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO table1 VALUES (1, '4');
INSERT INTO table1 VALUES (2, '3');

SELECT * FROM table1;

Which returns


+----+-------+
| id | field |
+----+-------+
|  1 | 4     |
|  2 | 3     |
+----+-------+

Which is correct.

So after removing the ORDER BY clause, EXPLAIN was more polite :


+----+---+----+------------+-----+-------+-----------+-----+-----------+
| id | [...] | type | possible_keys | key | key_len | ref | rows | Extra |
+----+---+----+------------+-----+-------+-----------+-----+-----------+
| 1 | [...] | ref | idx1, idx2 | idx2 | 8 | const, const | 2 | Using where |
+----+---+----+------------+-----+-------+-----------+-----+-----------+

And the query took a few milliseconds to run.

So next times you have slow queries, check your ORDER BY clause first and see of you can remove it or find a workaround.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)