Comments on: A challenge: partition a character set in MySQL http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Newbie http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11789 Newbie Thu, 21 Jun 2007 22:21:33 +0000 http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11789 Thanks for the explanation.

]]>
By: Dipin http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11787 Dipin Thu, 21 Jun 2007 19:03:22 +0000 http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11787 Expanding on Anonymous’ solution, couldn’t you do the following?

- fetch the first chunk
select min(name) as min_name, max(name) as max_name from ( SELECT name from table ORDER BY name LIMIT N) temp

- while there are other rows to be fetched
do
last_name = max_name from chunk[N-1]
select min(name) as min_name, max(name) as max_name from ( SELECT name from table WHERE name > last_name
ORDER BY name LIMIT N) temp

I haven’t tested this on all versions of MySQL but it seems to work on 5.0 where it leverages the index although it does end up using a derived table to temporarily hold the data from the index.

-Dipin

]]>
By: Xaprb http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11737 Xaprb Wed, 20 Jun 2007 01:16:54 +0000 http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11737 No, no, it’s a good question. It is efficient for several reasons:

1) WHERE foo > bar makes MySQL seek into the index and start the scan there. It doesn’t have to scan the rows before that in the index.

2) The rows are already sorted because I’ll use the primary key.

3) LIMIT N makes MySQL stop after it finds enough rows.

By contrast, OFFSET makes MySQL throw away OFFSET number of rows.

That said, I actually realized this won’t work in the particular situation I need it to — because the real query I’m using is grouped implicitly into a single row with MAX(), and LIMIT operates after grouping, not before. This is why I was thinking it needed to be a BETWEEN clause — I was thinking so hard about the problem I forgot the context.

There are other options, but still nothing magical is presenting itself. I’m going to work on some other things, where I have work lined up for myself, and see if something clever comes to me. It has happened in the past.

]]>
By: Newbie http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11714 Newbie Tue, 19 Jun 2007 00:53:38 +0000 http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11714 Sorry if I am asking a stupid question, but doesn’t “SELECT foo FROM table WHERE foo > bar ORDER BY foo LIMIT n” scan through the table, sort the rows and discard part of the results? How is it different from “LIMIT n OFFSET m” in terms of performance?

]]>
By: Anonymous http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11563 Anonymous Wed, 13 Jun 2007 20:18:55 +0000 http://www.xaprb.com/blog/2007/06/11/a-challenge-partition-a-character-set-in-mysql/#comment-11563 Hello

I’m glad to see you have liked my solution – yes it works only on
unique indexes (on a non-unique index the where clause name > last_name
may skip some rows), but it works for any data types.

I’ve found your articles very useful and instructive, keep up the good work!

C

]]>