Link to home
Start Free TrialLog in
Avatar of basil365
basil365Flag for Ireland

asked on

Optimising sql query

Hi,

I have a db with 2 main tables that represent data that we receive in a feed. In each of these 2 tables there is data that is duplicated so i normalised them into 4 separate tables. From a db storage point of view this works very well and saves me loads of space, but i'm having problems creating the query to load this data back into memory when required.

These are my tables:

tcache_b_active

bsourceId      int(11) PK
bId            int(11) PK
ipAddress        varchar(45)
custId           int(11)
channel          varchar(10)
bType          varchar(5)
bPlacedTime    datetime
bReceivedTime  datetime
amount        double
numLegs    int(11)

Open in new window


There is a one to one/many relationship with the following db (joined on bsourceId and bId)

tcache_b_leg_active

bsourceId      int(11) PK
bId            int(11) PK
globalId int(11) PK
tag              varchar(45)

Open in new window


There is also a one to one relationship with a customer table (bsourceId and customerId) :

tcache_customer

bsourceId      int(11) PK
customerId       int(11) PK
userName         varchar(100)
firstName        varchar(45)
lastName         varchar(45)
custRegCode      varchar(45)
custIsElite      tinyint(1)
custRegPostCode  varchar(45)
custRegEmail     varchar(150)
countryCode      varchar(10)

Open in new window


finally the tcache_b_leg table is joined to details table (on globalId)

tcache_b_details_active

globalId int(11) PK
Name       varchar(120)
description varchar(120)
startTime   datetime
result           varchar(1)

Open in new window


my query needs to get all the information from each of the tables when given a globalId. The problem arises because there can be multiple legs per main entry so i need an inner select. This is the query i have tried and it takes far too long.

select 
a.*,b.*,c.*,d.* from 
tcache_b_active a, tcache_customer b, tcache_b_leg_active c, tcache_b_details_active d
where
a.custId = b.customerId
and
a.bsourceId = b.bsourceId
and
a.bsourceId = c.bsourceId
and
c.globalId = d.globalId
and
a.bId in 
(
select bId from tcache_b_leg_active where globalId = $INPUT$
);

Open in new window


The database is mysql.

can you help me optimise this as much as possible or suggest another approach?

thanks
Avatar of YZlat
YZlat
Flag of United States of America image

#1 you should probably list all the fields you need, instead of using a.* or b.*. This way you will lessen the number of fields to display by avoiding display of some duplicate id fields

#2 make sure you use fully qualified table names, i.e. preceeded by schema name, etc.
you could also try using IF Exists instead of IN
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of basil365

ASKER

thanks for your help