473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Question on query efficiency

I've a db containing two tables which every item in table one is
linked to 1--200 items in table 2.
I want to generate a report like this:

Item in table 1
---------------------------------------
1st-related Item in table2
2nd related Item in table2
3rd related Item in table2
....
Nth related Item in table2
----------------------------------------
to do, I've generated a third table with a text-column to hold the
report text. Using an small procedure, I'v generated reports for all
items in table 1 and stored the text in the respective column.

Now, querying the third table take 100 times longer than joining
first and second tables.
the third table contains a single ID field which is a unique index.
the query for two table scenario and single table scenario are:
q1: select table2.* from table1 inner join table2 on table1.Id=
table2.Id where table2.id = <anid>

q2: select table3.* from table3 where Id = <anid>

Any Idea about the poor performance of the second query ?
Aug 10 '08 #1
9 2267
To have a better idea for the comparison of the queries, it would be
nice to have some information on the index structures of table1 and
table2. I assume they are also indexed based on their id columns. This
gives the query engine the option to construct the result set at the
same time when it uses the indexes to join and filter the id columns.
It's like a covering index and sql server takes good advantage of this
index combining feature.
When it comes to table3, there are two cons:
1. table3 is much more wider, because of the second column. Especially
if it exceeds 8K (which is the size of each page), then extended
reads occur to return the value from the second column.
2. And because the second column is not part of the index, it should
be read from the disk, while when two narrow tables were joined, as
mentioned above, the list of related items to form the second column
of the result set is likely to be already in the cache, when running
the query.

If the intenion is having a outline-style report which shows 1-200
items from table2 for each single item from table1, it would be worth
to consider this kind of outlining on the client side (e.e. using
reporting services) instead of creating table3.
Aug 11 '08 #2
To have a better idea for the comparison of the queries, it would be
nice to have some information on the index structures of table1 and
table2. I assume they are also indexed based on their id columns.
This
gives the query engine the option to construct the result set at the
same time when it uses the indexes to join and filter the id columns.
It's like a covering index and sql server takes good advantage of
this
index combining feature.

When it comes to table3, there are two cons:
1. table3 is much more wider, because of the text-column to hold the
report text. Especially if it exceeds 8K (which is the size of each
page), then extended
reads occur to return the value from that column.

2. And because the text-column to hold the
report text is not part of the index, it should
be read from the disk, while when two narrow tables were joined, as
mentioned above, the list of related items to form the report text
column
of the result set is likely to be already in the cache, while running
the query.

If the intention is having an outlined-style report which shows 1-200
items from table2 for each single item from table1, it would be worth
to consider this kind of outlining at middle tier or client side (e.g.
using
reporting services) instead of creating table3.
Aug 11 '08 #3
mansoorm (mu******@gmail .com) writes:
I've a db containing two tables which every item in table one is
linked to 1--200 items in table 2.
I want to generate a report like this:

Item in table 1
---------------------------------------
1st-related Item in table2
2nd related Item in table2
3rd related Item in table2
...
Nth related Item in table2
----------------------------------------
to do, I've generated a third table with a text-column to hold the
report text. Using an small procedure, I'v generated reports for all
items in table 1 and stored the text in the respective column.

Now, querying the third table take 100 times longer than joining
first and second tables.
the third table contains a single ID field which is a unique index.
the query for two table scenario and single table scenario are:
q1: select table2.* from table1 inner join table2 on table1.Id=
table2.Id where table2.id = <anid>

q2: select table3.* from table3 where Id = <anid>

Any Idea about the poor performance of the second query ?
First of all, it's not obvious why you should have this third table
in the first place.

Second, did you define any primary key for table3 that includes the id?

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Aug 11 '08 #4
1- I thought that generating the report text and storing it may speed
up the report generation upon request.
2-Yes ; all the tables have their id column defined as primary key.
Aug 12 '08 #5
Thanks for the comments. then the disk I/O is the problem ?
Aug 12 '08 #6
mansoorm (mu******@gmail .com) writes:
1- I thought that generating the report text and storing it may speed
up the report generation upon request.
It does not seem like it did.
2-Yes ; all the tables have their id column defined as primary key.
I think you need to post the CREATE TABLE statements and your queries.
Else we will just keep on guessing.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Aug 12 '08 #7
Mansoorm,
1- I thought that generating the report text and storing it may speed up the report generation upon request. <
The downside is that you always have to populate this third table and
if the underlying data in table one and two changed, your report is
outdated.

Report creators such as microsoft report viewer or crystal reports can
easily work without such an intermediate table. You would usually pass
something like

Table1Value | DependingTable2 Value

Then a grouping can be done in the front end (i. e. the report
creator).

brgds

Philipp Post

Aug 13 '08 #8
>I've a db containing two tables which every item in table one is linked [sic] to 1--200 items in table 2. <<

In RDBMS we have references; links is a concept from older network
databases.
> I want to generate a report like this:
Item in table 1
---------------------------------------
1st-related Item in table2
2nd related Item in table2 <<

Another conceptual error! Tables have no ordering, so ordinal
numbering makes no sense. That is from file systems or network
databases.
> I've generated a third table with a text-column to hold the report text. Using an small procedure, .. <<
The purpose of SQL is to return data. Period. It is not for
formatting it for display. You are still writing COBOL programs in
RDBMS. This is why we have report writers and front end
applications.
>the third table contains a single ID field [sic: columns are not fields] which is a unique index. <<
An index is not exposed to the user; did you mimic a sequential file
by using some silly auto-numbering scheme? There is no such thing as
a magical, universal "id" data element in a correct data model; each
identifier is for a *particular* kind of entity.

Please stop writing 1950's COBOL in SQL. What you have done is take
an automobile and hitched your old horse to it.
Aug 13 '08 #9
> 2-Yes ; all the tables have their id column defined as primary key. <<

Yep, you have no idea what RDBMS is and are still writing file systems
in SQL. Please stop coding until you can read a book or take a class
on the basics.

And if this magical, universal "id" is an IDENTITY it is not a column;
it is a physical table property, which you should call "physical
sequential insertion attempt count" instead.

Aug 13 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3115
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be...
96
5611
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries where surgeries.id = animals.id and only where the surgery date was date_a or date_b. I'm doing this in Microsoft Access 2000 and am tearing out my...
15
2302
by: Kapil Jain | last post by:
Dear All, What i need to achieve is : I am generating dynamic text boxes thru dhtml coding, i need onChange event of oragnistation text box i.e dynamically generated on click of "More" button in myhtml page in series manner like oragnisation0,oragnisation1,oragnisation2 and so on, and popup window will open onChange event of...
335
11553
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
7
2718
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both of the queries work fine when I use them to query the MySQL server directly. My guess is that the MySQL extension only expects a single resource...
4
1833
by: sqlservernewbie | last post by:
Hello, I'm trying to something that just works in Oracle, but does not in Sql Server. How can I get the percentage of two counts to be returned on each row of the query? select count(sid), /* all not the not null */ count(*),
2
5756
by: Jody | last post by:
Hi I've been working on a database which basically incorporates 3 tables to describe say a widget which is either sold or leased. I have the Widget table which stores the information related to the widget itself. I then have a WidgetSale table which stores only information related to the sale of the widget (advertised price, headline,...
1
1495
by: barrathi | last post by:
Hi all i need one query help! for reporting purpose i wrote one query - follows select s.tran_empid as EmpId, e.M_EMPL_NAME as EmpName, p.PROJ_NAME as Project, t.TITL_NAME as Title,CASE WHEN s.tran_param='Project Level Key Initiatives' THEN round((sum(s.tran_revscore)/(s.tran_weightage/100))) END as PLKI,CASE WHEN s.tran_param='Quality'...
4
1197
by: barrathi | last post by:
HAI ALL, for reporting purpose i wrote one query - follows select s.tran_empid as EmpId, e.M_EMPL_NAME as EmpName, p.PROJ_NAME as Project, t.TITL_NAME as Title, CASE WHEN s.tran_param='Quality' THEN round((sum(s.tran_revscore)/(s.tran_weightage/100))) END as Quality, CASE WHEN s.tran_param='Efficiency & Productivity' THEN...
3
11809
by: db55 | last post by:
How does the phrase "Is Not Null" in the where clause effect the effectiveness of a query? If it is a determent to the effectiveness of the query, how do you work around it? Thanks,
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.