You know you are doing too complex SQL queries...

ambs on 2009-06-29T21:24:16

You know you are doing too complex SQL queries... when DBI/DBD::SQLite refuses to parse the SQL statement.

The statement is simple: select a field, where it is one of a set of words:

SELECT word FROM dict WHERE word='aaaaa' OR word='bbbbb' OR word='ccccc' OR...

The first error was: parser depth of 1000 was reached.

No problem. Decided to construct a binary tree of ORs:

SELECT ... WHERE ((word = a or word = b) or (word = c or word = d))

Now, DBD::SQLite::db prepare failed: parser stack overflow.

I think the solution is to slipt this thing in more than one query. Damn!


how about using IN?

ddick on 2009-06-30T00:05:19

SELECT word FROM dict WHERE word IN ('aaaaa','bbbbb','ccccc',....)

Re:how about using IN?

ambs on 2009-06-30T10:57:25

This means I did not know SQL enough. Let me try it :)

Enough SQL Re:how about using IN?

n1vux on 2009-06-30T21:25:22

O'Reilly has some general titles in both portable paperback and online formats such as SQL Cookbook , SQL Hacks , SQL Pocket Guide, 2nd Ed , Head First SQL , SQL in a Nutshell, 3rd Ed . I mention these since they're not specific to any one vendor.

The DB vendor website should have online docs for how much of latest ANSI spec each release has implemented with what divergences (to be polite). I frequently have the MySQL website's manual section open when working on my dot.org site SQL, since their subquery syntax is their subset, not TIMTOWTDI allowed.

A fantastic free resource (which is in the corners specific to IBM DB2 but mostly standard ANSI SQL) is available for download or reference DB2 SQL Cookbook

Re:how about using IN?

ambs on 2009-06-30T11:21:57

It worked. THANK YOU!

Re:how about using IN?

htoug on 2009-07-02T06:58:18

I will (possibly) fail when the list of word in the 'IN'-clause gets long enough.
I had problems with clauses longer than approx 100_000 in an old version of Ingres.

The solution was to create a temporary table with all the words and then use this in the 'IN'-clause.

  CREATE TEMPORARY TABLE words(word VARCHAR(100));
  INSERT INTO words VALUES('aaaaa'); ....
  INSERT INTO words VALUES('xxxxx');
  SELECT word FROM dict WHERE word IN (SELECT word FROM WORDS);

(SQl-code is suboptimal, but you get the idea I hope).

Not that complex

Arador on 2009-06-30T10:56:51

That's not complex, just big. I once did a 18 fold join: that is much more complex (without using views it wouldn't have been manageable).