Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Perl DBI query of MS SQL with LIKE

by afoken (Chancellor)
on May 30, 2023 at 16:36 UTC ( [id://11152527]=note: print w/replies, xml ) Need Help??


in reply to Perl DBI query of MS SQL with LIKE

Just a note on WHERE column LIKE 'something%', if this is really what you want:

Using LIKE with only a trailing % is usually slower than using SUBSTR, or at least it was WAY slower the last time I had to do serious work with relational databases. It boils down to the query optimizer not being able to use indices on the queried column with LIKE, resulting in a full table scan. If you use SUBSTR instead, your query can be optimized to use indices and thus does not have to scan the entire table.

So, unless SQL query optimizers have become much smarter than they were a few years ago, WHERE SUBSTR(column, 1, 9) = 'something' should be faster.


Using DBI, there are basically three ways to pass the length of the search term to SUBSTR:

If you can use numbered or named placeholders (depends on your database driver), you simply reuse the placeholder:

# numbered: my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LEN +GTH(:1)) = :1'); $sth->bind_param(1, $what); $sth->execute(); # named: my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LEN +GTH(:what)) = :what'); $sth->bind_param(':what', $what); $sth->execute();

If you are limited to simple ? placeholders, you can pass the same value twice:

my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, LENGTH(?)) = ?'); $sth->bind_param(1, $what); $sth->bind_param(2, $what); $sth->execute(); # or without explicit bind_param: $sth->execute($what, $what); </c>

You could also calculate the length in perl. This should normally work, unless your data contains Unicode AND perl, DBI, DBD, and the database don't agree on how your data is really encoded (i.e. you messed up the Unicode handling):

my $sth=$dbh->prepare('SELECT ... FROM ... WHERE SUBSTR(column, 1, ?) += ?'); $sth->execute(length($what), $what);

Note: This will cause interesting results if Unicode handling is messed up ...


Oh, by the way: What happens if $transactionCodeQuery contains a %? If you can guarantee it does not and will never, don't mind. If $transactionCodeQuery is user input, try harder to avoid using LIKE. If there is no way around LIKE, try using ... LIKE ... ESCAPE ....

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: Perl DBI query of MS SQL with LIKE
by Bod (Parson) on May 30, 2023 at 17:24 UTC
    Using LIKE with only a trailing % is usually slower than using SUBSTR

    I cannot speak for SQL Server but with MariaDB LIKE is optimised to use indexes as long as the first character is not a wildcard - '%' or '_'

    See documentation

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found