TABLE_CATALOG | TABLE_SCHEMA |
TABLE_NAME | COLUMN_NAME |
ORDINAL_POSITION | COLUMN_DEFAULT |
IS_NULLABLE | DATA_TYPE |
CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH |
NUMERIC_PRECISION | NUMERIC_PRECISION_RADIX |
NUMERIC_SCALE | CHARACTER_SET_NAME |
COLLATION_NAME | TYPE_NAME |
NULLABLE | IS_COMPUTED |
SELECTIVITY | CHECK_CONSTRAINT |
SEQUENCE_NAME | REMARKS |
SOURCE_DATA_TYPE |
| 'TEST' | 'PUBLIC' | 'OM' | 'B' | 1 | '0' | 'NO' | 4 | 10 | 10 | 10 | 10 | 0 | 'Unicode' | 'OFF' | 'INTEGER' | 0 | false | 50 | '' | null | '' | null |
| 'TEST' | 'PUBLIC' | 'OM' | 'PK' | 2 | null | 'YES' | -5 | 19 | 19 | 19 | 10 | 0 | 'Unicode' | 'OFF' | 'BIGINT' | 1 | false | 50 | '' | null | '' | null |
Also I typod '500,00' but meant the table has '500,000' rows.
Also to be clearer, the 'pk' column is a primary key and so only about
40 items in the set matched the query; even so adding the second AND-
IN seems to slow it down exponentially as that list grows; its as if
the entire dataset and not subset is searched for each value in each
list.
I would recommend using subqueries.
Regards,
j
> --
> 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.
> For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
>
>
Thomas I would appreciate the opportunity to help any way I can to
improve this (except I'm afraid monetarily); is it an issue you are
aware of and if so do you have a plan or test build I could try, or
other way (test cases, code todo etc) I could help?
Thanks,
Ken
On Feb 23, 12:41 pm, Jan Kotek <openco...@gmail.com> wrote:
> Hi,
> I had simular problem. H2 does not optimize AND conditions very well.
>
> I would recommend using subqueries.
>
> Regards,
> j
>
SELECT objId,b FROM om WHERE
b IN (1,2) AND objId IN (1254619816788000000,1176099280681000000)
H2 was started with:
java -Xrunhprof:cpu=time,depth=8 -Xmx256M -DRETURN_LOB_OBJECTS=false -
jar h2-1.2.128.jar org.h2.tools.Server -webAllowOthers -tcpAllowOthers
-tcpPort 9101&
(note this profiling mode took enough time that I needed to send a
term signal to java, before the query completed)
I found the problem, it will be fixed in the next release. The index
is currently not used if there were multiple IN(..) conditions. A
workaround is to use OR:
SELECT pk FROM om WHERE pk IN (<BIGINT-LIST>) AND (b = 1 OR b = 2)
The query plan is not very clear. I will change that as well, so that
the query plan will contain ".tableScan" if a table scan is used.
Regards,
Thomas
This query now runs super fast, <15ms (sweet!!!):
SELECT objId,b FROM om WHERE
(b=1 OR b=2)
AND objId IN (1254619816788000000,1176099280681000000)
But this query still takes >10 seconds:
SELECT objId,b FROM om WHERE
(b=1 OR b=2)
AND (objId=1254619816788000000 OR objId=1176099280681000000)
Will your solution account for this difference also?
Again many thanks!!!
Ken
On Feb 24, 11:12 pm, Thomas Mueller <thomas.tom.muel...@gmail.com>
wrote:
Could you try again with version 1.2.130?
Regards,
Thomas
<10ms:
SELECT * FROM om WHERE
(b=26 OR b=27)
AND numval = 468203
But ~2000ms:
SELECT * FROM om WHERE
b IN (26,27)
AND numval = 468203
where 'numval' is a indexed double type (which I recently added)...
If I remove either of the AND conditions, the speed query again
executes very fast...
Let me know if you need a profiler output..
The optimizer doesn't choose the best index if there are multiple.
This is a limitation that is quite hard to fix currently. I will add a
feature request. My test case is:
drop table test;
create table test(b double, numval int);
insert into test
select mod(x, 10), x from system_range(1, 1000);
explain SELECT * FROM test
WHERE b IN (26,27) AND numval = 468203;
-- tableScan
create index idx_b on test(b);
explain SELECT * FROM test
WHERE b IN (26,27) AND numval = 468203;
-- IDX_B: B IN(26, 27)
create index idx_numbal on test(numval);
explain SELECT * FROM test
WHERE b IN (26,27) AND numval = 468203;
-- IDX_B: B IN(26, 27)
Regards,
Thomas
based on speed of the query, it seems to be using scans, not indexes.
SELECT OM.OMATTRIBID, OM.OBJID, OM.OWNERID, OM.OBJVALUE, OM.LISTORDER, OM.OMID FROM PUBLIC.OM /* PUBLIC.OM_ATTRVAL_ID: OMATTRIBID IN(26, 27) AND OBJVALUE = '468203' */ WHERE (OMATTRIBID IN(26, 27)) AND (OBJVALUE = '468203')
-During the query all CPU is user, not wa (swap).
COLUMN_NAME | DATA_TYPE | SELECTIVITY | |
| 1 | 'OMATTRIBID' | 4 | 1 |
| 2 | 'OBJID' | -5 | 5 |
| 3 | 'OWNERID' | 4 | 1 |
| 4 | 'OBJVALUE' | 12 | 12 |
| 5 | 'LISTORDER' | 4 | 1 |
| 6 | 'OMID' | -5 | 100 |
explain analyze SELECT COUNT(*) FROM om WHERE objvalue='468203' AND (OMATTRIBID=26 OR OMATTRIBID=27)
SELECT COUNT(*) FROM PUBLIC.OM /* PUBLIC.OM_ATTRVAL_ID: OMATTRIBID IN(26, 27) AND OBJVALUE = '468203' */ /* scanCount: 2181921 */ WHERE (OMATTRIBID IN(26, 27)) AND (OBJVALUE = '468203') /* total: 34990 OM.OM_ATTRVAL_ID read: 34990 (100%) */