473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2259
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****@sommarskog.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****@sommarskog.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 | DependingTable2Value

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
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...
96
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...
15
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...
335
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
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...
4
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),...
2
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...
1
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...
4
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...
3
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.