Sorting with index

76 views
Skip to first unread message

Petr Holik

unread,
Jun 7, 2012, 10:04:12 AM6/7/12
to H2 Database
Hello,

I am little bit unsure when index is used for sorting and when not. I
have two almost same SQL first uses sort using index and second not.
The only difference is in DESC keyword.

the SQL is as follows:

select * from bills where closedtime < '2012-06-08 12:04:36.343 ' AND
CLOSED = TRUE order by closedtime limit 1
select * from bills where closedtime < '2012-06-08 12:04:36.343 ' AND
CLOSED = TRUE order by closedtime DESC limit 1

of course i can supply explain analyze if needed. Second query on same
data is about 10 - 100 times slower(does not use index for sorting)

Where is mistake or am I missing something?

Sincerely Petr Holik

Noel Grandin

unread,
Jun 8, 2012, 7:56:45 AM6/8/12
to h2-da...@googlegroups.com, Petr Holik
H2 doesn't support stepping through an index backwards, you would need
to create a reverse index to support that.

Something like :

create index idx_id_desc on test(id desc)

patrice henrio

unread,
Jun 8, 2012, 8:10:32 AM6/8/12
to h2-da...@googlegroups.com
I can give my experience  (not big, 2 or 3 M) with à complex request sorting on a column : without index more than 15 minutes, with index 7 seconds. 

2012/6/8 Noel Grandin <noelg...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.




--
Si vous vous intéressez à l'histoire, vous pouvez parcourir mon site : http://histoiremondiale.free.fr

Petr Holik

unread,
Jun 8, 2012, 12:11:38 PM6/8/12
to h2-da...@googlegroups.com, Petr Holik
Hello Noel, 
thanks for, reply your sugestion works. BUT
I do not see the reason why H2 does not support this? Binary tree which is used in H2 can support stepping backward and forward, am I right?
Should not be this enhancement request? It is very often request in web application that user can user sort data ASC or DESC...

Or there should be updated documentation in crate index syntax. There is no word about fact that index can be crated on DESC column.

Petr

Rami Ojares

unread,
Jun 8, 2012, 12:34:56 PM6/8/12
to h2-da...@googlegroups.com
Something I have been also wondering.

- rami

Noel Grandin

unread,
Jun 8, 2012, 4:03:19 PM6/8/12
to h2-da...@googlegroups.com, Petr Holik
On Fri, Jun 8, 2012 at 6:11 PM, Petr Holik <hol...@gmail.com> wrote:
> Should not be this enhancement request? It is very often request in web
> application that user can user sort data ASC or DESC...
>

Patches are welcome, the code is open-source.....

> Or there should be updated documentation in crate index syntax. There is no
> word about fact that index can be crated on DESC column.
>
I will see what I can do about updating the documentation,

Thomas Mueller

unread,
Jun 11, 2012, 3:42:18 AM6/11/12
to h2-da...@googlegroups.com
Hi,

Reading in descending order is possible now, but I guess it will be much slower.

Regards,
Thomas

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.

Rami Ojares

unread,
Jun 11, 2012, 3:48:28 AM6/11/12
to h2-da...@googlegroups.com
Hooray!
Since which version?
167 or an upcoming?

- Rami

Thomas Mueller

unread,
Jun 18, 2012, 2:40:02 PM6/18/12
to h2-da...@googlegroups.com
Hi,

Hooray!
Since which version?
167 or an upcoming?

It's possible since quite a long time actually (function Cursor.previous()). However, this is not used internally to support "order by desc". It is only used for a few edge cases (select max(...) if there are null entries at the end). It would not perform well for regular index scans I believe.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages