Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Databasing
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 10-23-2009, 07:08 AM
  #1
Katie6
Novice (Level 1)
 
Join Date: Aug 2008
Posts: 9
iTrader: (0)
Katie6 is an unknown quantity at this point
How can I make my SQL query more efficient?

Hi there,

I have a SQL query which keeps timing out when I run it and I'm hoping that there's a more efficient way of doing it which will prevent this from happening.

About the query
In the company I work for, they measure the number of hits per article. The URL and hits are recorded in one table and the title and category of the article etc is stored in another. Also, the stats have been set up so that there is a new table of hits for each month.

So, I have 12 tables, and I want to stick them together, one on top of the other - to get all the hits in one big table. Then, I'd like to join the title, category etc onto that big hits table and group everything so that I get the number of hits per URL.

Here's the query. It works if I put TOP 5 in every select statement, but I get a [Microsoft][ODBC SQL Server Driver]Timeout expired message if I select whole tables.

Code:
SELECT     *, COUNT(*) AS Hits
FROM         (SELECT     All_News.URL AS URL, dbo.Manu_News.Primary_Topic AS Category, dbo.Manu_News.Message_Title AS Title, 
                                              dbo.Manu_News.Message_Date AS PublishDate
                       FROM          (SELECT     dbo.news_stats_1.url AS URL, dbo.news_stats_1.messageID AS messageID
                                               FROM          dbo.news_stats_1
                                               UNION ALL
                                               SELECT     dbo.news_stats_2.url AS URL, dbo.news_stats_2.messageID AS messageID
                                               FROM         dbo.news_stats_2
                                               UNION ALL
                                               SELECT     dbo.news_stats_3.url AS URL, dbo.news_stats_3.messageID AS messageID
                                               FROM         dbo.news_stats_3
                                               UNION ALL
                                               SELECT     dbo.news_stats_4.url AS URL, dbo.news_stats_4.messageID AS messageID
                                               FROM         dbo.news_stats_4
                                               UNION ALL
                                               SELECT     dbo.news_stats_5.url AS URL, dbo.news_stats_5.messageID AS messageID
                                               FROM         dbo.news_stats_5
                                               UNION ALL
                                               SELECT     dbo.news_stats_6.url AS URL, dbo.news_stats_6.messageID AS messageID
                                               FROM         dbo.news_stats_6
                                               UNION ALL
                                               SELECT     dbo.news_stats_7.url AS URL, dbo.news_stats_7.messageID AS messageID
                                               FROM         dbo.news_stats_7
                                               UNION ALL
                                               SELECT     dbo.news_stats_8.url AS URL, dbo.news_stats_8.messageID AS messageID
                                               FROM         dbo.news_stats_8
                                               UNION ALL
                                               SELECT     dbo.news_stats_9.url AS URL, dbo.news_stats_9.messageID AS messageID
                                               FROM         dbo.news_stats_9
                                               UNION ALL
                                               SELECT     dbo.news_stats_10.url AS URL, dbo.news_stats_10.messageID AS messageID
                                               FROM         dbo.news_stats_10
                                               UNION ALL
                                               SELECT     dbo.news_stats_11.url AS URL, dbo.news_stats_11.messageID AS messageID
                                               FROM         dbo.news_stats_11
                                               UNION ALL
                                               SELECT     dbo.news_stats_12.url AS URL, dbo.news_stats_12.messageID AS messageID
                                               FROM         dbo.news_stats_12) All_News LEFT OUTER JOIN
                                              dbo.Manu_News ON All_News.messageID = dbo.Manu_News.Message_Id) DERIVEDTBL
GROUP BY URL, Category, Title, PublishDate
It would be great if anyone can suggest a solution to this!

Many thanks,

Katie
Katie6 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-27-2009, 12:06 PM
  #2
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,410
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
Have you tried "explain"?
That would be my first thing to do.
Can we see the output of explain?
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 06:32 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.