Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: "Can't use index" with DBD::SQLite

by choroba (Chancellor)
on Jan 29, 2013 at 21:12 UTC ( #1015933=note: print w/replies, xml ) Need Help??

in reply to "Cannot use index" with DBD::SQLite

How did you create the table and the index? Does the index really index the "name" column? This works for me:
#!/usr/bin/perl use warnings; use strict; use DBI; unlink 'test.db' or warn $!; my $dbh = DBI->connect('dbi:SQLite:test.db', q(), q()); $dbh->do(q(create table profile (profile_id integer, name varchar))); $dbh->do(q(create index idx_name on profile (name, profile_id))); my $insert = $dbh->prepare(q(insert into profile values(?, ?))); $insert->execute(@$_) for [10, 'Joseph'], [11, 'William'], [12, 'Elisabeth']; my $sql = q{ SELECT profile_id FROM profile INDEXED BY idx_name WHERE name == ? }; my $sth = $dbh->prepare($sql); my $name = 'William'; $sth->execute($name); my $arr = $sth->fetchall_arrayref; print "@$_\n" for @$arr;
If I switch name and profile_id in the index creation, I get the error you mentioned, but only if no other index has been yet defined. I am not sure this is the intended behaviour.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: "Can't use index" with DBD::SQLite
by LonelyPilgrim (Beadle) on Jan 29, 2013 at 22:50 UTC

    Thanks. I ran your code, and it worked for me, and then re-created my index per your specifications, with the one tweak I made before. It still didn't work on my table. So I figured that tweak must be the problem.

    For some reason I don't understand, it didn't like my COLLATE NOCASE DESC specification. When I re-created the index once again without that, it started working. Huh.

      When you give the COLLATE NOCASE DESC option in the index it means convert all the text to lower case and store them in descending order for case insensitive search. So, to make use of that index, you have to give the same collation again in your query, and the following should work.

      SELECT profile_id FROM profile INDEXED BY idx_name WHERE name == ? COL +LATE NOCASE

      If you recreated the index without this collation, then it means you cannot do case insensitive search, or have to convert things to upper or lower case every time like below.

      SELECT profile_id FROM profile INDEXED BY idx_name WHERE lower(name) = += lower(?)

      Although you have found a workaround, this may be useful for someone else, facing a similar problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015933]
and all is calm...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-08-24 03:47 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (363 votes). Check out past polls.