Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by choroba (Abbot)
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.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: "Can't use index" with DBD::SQLite
Select or Download Code
Re^2: "Can't use index" with DBD::SQLite
by LonelyPilgrim (Sexton) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-12-20 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls