/    Sign up×
Community /Pin to ProfileBookmark

improving search time of a query

i have this query :

PHP:
——————————————————————————–

select distinct order_header.order_id
from order_header
LEFT JOIN order_rows on order_header.order_id=order_rows.order_id
where
( (TAARICH_HASPAKA >= 1112853600 AND TAARICH_HASPAKA <= 1113026399 AND (sent=’0′ OR sent=”)) OR (TAARICH_HASPAKA < 1112853600 AND TEUDAT_MISH=” AND (sent=’0′ OR sent=”) ) OR (TAARICH_TEUDA >= 1112853600 AND TAARICH_TEUDA <= 1112939999))
and region_id=’002′


——————————————————————————–

when i run this query withought the left join i get results in seconds.
when i add the left join it takes 3 times more!
when i remove the

quote:
——————————————————————————–


and region_id=’002′
——————————————————————————–

(which means go over all the regions it takes me 2-3 minutes at least!!!!)
currently i have in the order_header 50000 lines and in the order_rows i have 200000 lines!
i added some indexes but no luck
still everything is very very slow
what can i do?
thnaks in advance
peleg

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 07.2005 — Don't know how much time this might save, but working under the idea that a series of AND comparisons will fail as soon as one FALSE is found so do the simple tests first, and combining the test for 'send' which was present in two places, I came up with this version of your where clause (which I THINK is functionally identical):
<i>
</i>WHERE
region_id='002' AND
(
(sent='0' OR sent='') AND
(TAARICH_HASPAKA &gt;= 1112853600 AND TAARICH_HASPAKA &lt;= 1113026399) OR
(TAARICH_HASPAKA &lt; 1112853600 AND TEUDAT_MISH='')
) OR
(TAARICH_TEUDA &gt;= 1112853600 AND TAARICH_TEUDA &lt;= 1112939999)
Copy linkTweet thisAlerts:
@pelegk1authorApr 07.2005 — this is slower and withwrong results
Copy linkTweet thisAlerts:
@DaiWelshApr 07.2005 — <i>
</i>WHERE
region_id='002' AND
(
(sent='0' OR sent='') AND
(TAARICH_HASPAKA &gt;= 1112853600 AND TAARICH_HASPAKA &lt;= 1113026399) OR
(TAARICH_HASPAKA &lt; 1112853600 AND TEUDAT_MISH='')
) OR
(TAARICH_TEUDA &gt;= 1112853600 AND TAARICH_TEUDA &lt;= 1112939999)
[/QUOTE]


Missing a couple of sets of brackets required to retain the original meaning I think, perhaps

<i>
</i>WHERE
region_id='002'
AND
(
(TAARICH_TEUDA &gt;= 1112853600 AND TAARICH_TEUDA &lt;= 1112939999)
OR
(
(sent='0' OR sent='')
AND
(
(TAARICH_HASPAKA &gt;= 1112853600 AND TAARICH_HASPAKA &lt;= 1113026399)
OR
(TAARICH_HASPAKA &lt; 1112853600 AND TEUDAT_MISH='')
)
)
)
Copy linkTweet thisAlerts:
@crh3675Apr 07.2005 — The distinct is also going to kill your processing power. Try grouping the results. Also, use the table names for the where clauses so that MySQL doesn't have to do that work for you. I slightly modified the code. Don't know if it will be faster but moving the region_id='002' to the top may help

<i>
</i>SELECT order_header.order_id
FROM order_header
LEFT JOIN order_rows on order_header.order_id=order_rows.order_id
WHERE region_id='002' and
(
((TAARICH_HASPAKA BETWEEN 1112853600 AND 1113026399) AND (sent='0' OR sent=''))
OR
(TAARICH_HASPAKA &lt; 1112853600 AND TEUDAT_MISH='' AND (sent='0' OR sent=''))
OR
(TAARICH_TEUDA BETWEEN 1112853600 AND 1112939999)
)
GROUP BY order_header.order_id


I don't know which columns belong to which tables so you will have to add the table info for each column
×

Success!

Help @pelegk1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...