Re: SQL code question

From: Gordon E. Hooker <gordonh_at_acslink.net.au>
Date: 1995/08/12
Message-ID: <40h21e$j8o_at_dingo.cc.uq.oz.au>#1/1


awdorrin_at_ictest.delcoelect.com (Albert W. Dorrington) wrote:

Albert,

The moment you introduce the substr function you query will do a sequential scan of the database. Your index is not helping at all.

>Hi,
 

> I'm fairly new to coding SQL, but do understand the
>basics. However, I've recently written a segment of SQL which
>seems to take a lot longer to execute than I would think it
>should. I'm hoping that someone on here might be able to point
>out a more efficient way of writing this query.
 

>The query:
>====
 

>SELECT
> '$thing' PART_TYPE,
> (count(distinct(substr(TABLEB.rowid,1,8)||substr(TABLEB.rowid,15,4))))*512
> Bytes,
> count(*) Row_Count
>FROM
> TABLEA, TABLEB
>WHERE
> test_area = '$area'
> AND
> TABLEA.master_id = TABLEB.master_id
> AND
> TABLEB.master_id BETWEEN $time1 AND $time2
> AND
> part_type = '$thing';
 

>====
 

>TABLEA is mostly a 'table of contents' for the data, while
>TABLEB contains data of a more specific nature.
 

>The 'master_id' column (which is common to both tables) is a
>Unix 'seconds' timestamp (you know, the number of seconds since
>Jan 1, 1970) and it does have an index applied.
 

>The columns 'test_area' and 'part_type' are both in TABLEA.
 

>Of course, $thing, $area, $time1 and $time2 are variables, that
>would be substitued.
 

>Basically the query is trying to determine (estimate) how many bytes
>of data a given part type, from a certain test area, is taking up in
>TABLEB over a certain time interval.
 

>Any advice or suggestions would be greatly appreciated.
 

>Thanks,
 

>--
>Al Dorrington
>awdorrin_at_ictest.delcoelect.com Database Admin
>Delco Electronics - IC CIM Unix Sysadmin
>Kokomo, Indiana, USA Phone: 317.451.9655

-----------------------------\ooOoo/-----------------------------------
Gordon Hooker MACS PCP                                     ,--_|\
25 Clarke Street, Ripley, Queensland, 4306, Australia     /      \ 
gordonh_at_acslink.net.au                                    \_.--._/
mobile: 018883835 phone: 61-7-2889716                           V
-----------------------------------------------------------------------
The opinions you read here are mine and not necessarily those of my employers. Although, I must say they are the truth, the whole truth, and nothing like the truth. As far as I'm concerned anyway... Received on Sat Aug 12 1995 - 00:00:00 CEST

Original text of this message