getting the 3 queries as optimised single query.. is this possible

Reply

Join Date: Apr 2005
Posts: 183
Reputation: hbmarar is an unknown quantity at this point 
Solved Threads: 0
hbmarar's Avatar
hbmarar hbmarar is offline Offline
Junior Poster

getting the 3 queries as optimised single query.. is this possible

 
0
  #1
Nov 21st, 2008
hi experts,

Is it possible that i could get the 3 queries made into one single query.Any help could make me understand and correct myelf in better queries. Each query is taking long time and sometimes the first query crashes , with

> show processlist

showing status as 'copying to tmp data'

Thanks for concern and consideration

Thanks.

Saff

  1. CREATE TABLE `yk_contributor_aging` (
  2. `contributor_id` INT(10) NOT NULL DEFAULT '0',
  3. `descale` FLOAT DEFAULT NULL,
  4. `rating_credit` FLOAT NOT NULL DEFAULT '0',
  5. `comment_credit` FLOAT NOT NULL DEFAULT '0'
  6. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

---------------------------------------------------------------------------------------------------------
Query below inserts the contributor id and descale columns ( takes too much time that hangs )
---------
  1. INSERT INTO `yk_contributor_aging` ( contributor_id, descale) SELECT yk_contribution.contributor_id, SUM(LOG(7, 365)-LOG(7, DATEDIFF(NOW(), yk_contribution.contribution_date ))) as descale FROM yk_contribution,yk_rating, yk_entity_score WHERE yk_entity_score.entity_key = yk_contribution.key AND DATEDIFF(NOW(), yk_contribution.contribution_date ) <= 365 GROUP BY yk_contribution.contributor_id ORDER BY yk_contribution.contributor_id ;
----------------------------------------------------------------------------------------------------------
Query below selects comment credit and is written to sql file for inserts the comment_credit column in yk_contributor_aging table
  1. SELECT contribution_date, cor.contributor_id, ( LOG(10,SUM(((LOG(7, 365) - LOG(7,DATEDIFF(NOW(), contribution_date )))/(LOG(7,365))))) / 3) as credit FROM yk_contribution as cion,yk_contributor as cor WHERE cor.contributor_id=cion.contributor_id GROUP BY cion.contributor_id ORDER BY cion.contributor_id;
-----------------------------------------------------------------------------------------------------------
Query below selects comment credit and is written to sql file for inserts the comment_credit column in yk_contributor_aging table
  1. SELECT rating.contributor_id, ( LOG(10,SUM(((LOG(7, 365) - LOG(7,DATEDIFF(NOW(), rating_date )))/(LOG(7,365))))) / 4) as credit FROM yk_rating as rating, yk_contributor as cor WHERE cor.contributor_id = rating.contributor_id GROUP BY contributor_id ORDER BY contributor_id;
Last edited by peter_budo; Nov 28th, 2008 at 12:13 pm. Reason: Create table uses sql too, so hence the code tags :-)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: varmadba is an unknown quantity at this point 
Solved Threads: 8
varmadba varmadba is offline Offline
Junior Poster in Training

Re: getting the 3 queries as optimised single query.. is this possible

 
0
  #2
Dec 1st, 2008
Rather than Joining all 3 into 1 and complicating things u can add some index to tables and optimize select queries to executing will be faster

  1. INSERT INTO `yk_contributor_aging` ( contributor_id, descale) SELECT yk_contribution.contributor_id, SUM(LOG(7, 365)-LOG(7, DATEDIFF(NOW(), yk_contribution.contribution_date ))) AS descale FROM yk_contribution,yk_rating, yk_entity_score WHERE yk_entity_score.entity_key = yk_contribution.KEY AND DATEDIFF(NOW(), yk_contribution.contribution_date ) <= 365 GROUP BY yk_contribution.contributor_id ORDER BY yk_contribution.contributor_id ;

  1. DATEDIFF(NOW(), yk_contribution.contribution_date ) <= 365

Above part of query should be changed,even if you have a index on
yk_contribution.contribution_date database will not use,it will simply by pass index and will do a full table scan
its better to pass the date/time value to it

Add some indexes and optimize selects so executing will be faster
that's the better in my view

Note Function are always better only above the from clause
:- Varma

We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum


Views: 627 | Replies: 1
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC