473,386 Members | 1,883 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.

Table Scans - more rows brought back than in table

I have a query which is quite complex. It's based on a set of data in
a complex view which takes the data from several tables.

In this complex query, if I allow the various parts of the query to
work on the results of the view (MISView), it can take 15 minutes to
run (eek !), however, if I create a temporary table with the data from
the view and then use that for the remainder of the query, it runs in
approx 20 seconds.

Now, I have examined the execution plan (my new favourite toy) and
there is a difference (as expected). However when looking at the part
of the query that takes up most of the time, it shows that it brings
back 109,645,866 records from a table (Credit) that contains 13,002
records. This table is one that is referenced in the view (MISView)
which contains 13,653 records and does get some of it's data from the
table which is scanned (Credit).

For the record, we don't have any tables with over 100,000 records in,
so 109 million rows is going some for us. The part of the query that
runs slow does reference another copy of itself but this is necessary
for the equation that is being run.

Now I'm OK with why it's doing the table scan, but why does it bring
back substantially more data than is in the table ? Is it some
multiple of the number of records that it's trying to work out. I
assume it tries to run a seperate plan for the view as part of it's
process.

Ideally, I'm still going to go down the route of the temporary table,
but I would like to understand more about what it does first as I
don't like leaving things unanswered.

Any help would be appreciated.
Jul 20 '05 #1
6 2178
"Ryan" <ry********@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
I have a query which is quite complex. It's based on a set of data in
a complex view which takes the data from several tables.

In this complex query, if I allow the various parts of the query to
work on the results of the view (MISView), it can take 15 minutes to
run (eek !), however, if I create a temporary table with the data from
the view and then use that for the remainder of the query, it runs in
approx 20 seconds.

Now, I have examined the execution plan (my new favourite toy) and
there is a difference (as expected). However when looking at the part
of the query that takes up most of the time, it shows that it brings
back 109,645,866 records from a table (Credit) that contains 13,002
records. This table is one that is referenced in the view (MISView)
which contains 13,653 records and does get some of it's data from the
table which is scanned (Credit).

For the record, we don't have any tables with over 100,000 records in,
so 109 million rows is going some for us. The part of the query that
runs slow does reference another copy of itself but this is necessary
for the equation that is being run.

Now I'm OK with why it's doing the table scan, but why does it bring
back substantially more data than is in the table ? Is it some
multiple of the number of records that it's trying to work out. I
assume it tries to run a seperate plan for the view as part of it's
process.

Ideally, I'm still going to go down the route of the temporary table,
but I would like to understand more about what it does first as I
don't like leaving things unanswered.

Any help would be appreciated.


Posted and mailed
You need to tell us what version of SQL Server, DDL for table definitions,
the view, the procedures that you are using, INSERT statements for sample
data and the expected output. Without a lot more information, it is
impossible to tell what is going on.

Wild-ass guess:
Somewhere in your process you are creating a Cartesian product from your
tables hence the large result set.
Which means you are missing some equality condition between tables or some
limiting condition isn't being coded.

HTH
Ronnie
Jul 20 '05 #2
Ryan (ry********@hotmail.com) writes:
Now, I have examined the execution plan (my new favourite toy) and
there is a difference (as expected). However when looking at the part
of the query that takes up most of the time, it shows that it brings
back 109,645,866 records from a table (Credit) that contains 13,002
records. This table is one that is referenced in the view (MISView)
which contains 13,653 records and does get some of it's data from the
table which is scanned (Credit).


If you are talking about the graphical showplan, that is an
estimated row count.

Why it is so high I don't know, as you did not include any tables,
SQL, view defitinion or query plan. But it could be high, if the
plans involves several passes of the table.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Are you talking about logical I/O's? If so that is the number of times
SQL hits a page, not number of records. As some of the other guys have
already stated, need more info.

Ray Higdon MCSE, MCDBA, CCNA

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
OK, a little more info. SQL below. This is the version that takes
forever. If I take the data from the view MISData into a temporary
table which I use in the later part it then speeds up. FWIW this is
only a small part of the SQL but the relevant bit. The MIS view is
complex so, if it helps to have the structure, I can post it.

Thanks

Ryan
CREATE TABLE #Params (
CurrentYear Integer NULL,
CurrentPeriod Integer NULL)

/*+++++++++++++++++++*/

INSERT INTO
#Params

SELECT
CurrentYear = 2003,
CurrentPeriod = 6

/*+++++++++++++++++++*/

SELECT DISTINCT
c2.client,
c2.element,
c2.ConHierLevDes3,
c2.ConHierLevDes1,
c2.proddesc,
c2.classdesc,
c2.freq,
c2.periodno,
sum(gross-payaway) AS CommTotal,
1 AS Cases

INTO
#Temp
-- #Temp is used later on so this is still relevant at this stage.

FROM
MisData c2, #Params qp

WHERE
c2.yearno = qp.CurrentYear AND
c2.periodno <= qp.CurrentPeriod AND
c2.gross <> 0.000 AND
(SELECT Sum(x.gross - x.payaway) FROM
MisData x
WHERE x.policy_contract_number = c2.policy_contract_number AND
x.PeriodNo <= qp.CurrentPeriod
GROUP BY x.policy_contract_number) <> 0.0000

GROUP BY
c2.Client,
c2.Element,
c2.ConHierLevDes3,
c2.ConHierLevDes1,
c2.ProdDesc,
c2.ClassDesc,
c2.PeriodNo,
c2.Freq

Ray Higdon <ra*******@higdonconsulting.com> wrote in message news:<3f*********************@news.frii.net>...
Are you talking about logical I/O's? If so that is the number of times
SQL hits a page, not number of records. As some of the other guys have
already stated, need more info.

Ray Higdon MCSE, MCDBA, CCNA

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #5
Forgot to mention, SQL 7.0 is what I'm using :-)

Ray Higdon <ra*******@higdonconsulting.com> wrote in message news:<3f*********************@news.frii.net>...
Are you talking about logical I/O's? If so that is the number of times
SQL hits a page, not number of records. As some of the other guys have
already stated, need more info.

Ray Higdon MCSE, MCDBA, CCNA

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #6
Ryan (ry********@hotmail.com) writes:
OK, a little more info. SQL below. This is the version that takes
forever. If I take the data from the view MISData into a temporary
table which I use in the later part it then speeds up. FWIW this is
only a small part of the SQL but the relevant bit. The MIS view is
complex so, if it helps to have the structure, I can post it.


I see that you query refer to the view twice. And if as you say that
the view is complex, I'm not surprised that materializing the view
into a temp table has as an enourmous payoff.

Had you been on SQL 2000, you would have had the option to materialise
the view on perstitant basis by making it an indexed view.

Since you are on SQL7, you may have found the best solution. Unless
you want to replace the reference to the view with actual query behind
and see what you can simplify.

Views are a bit dangerous. You can be lead to use a view which looks
good, but which includs references to tables you don't need at all
for your query. In fact, the system I work with does not have any
views at all.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #7

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

Similar topics

32
by: Rich | last post by:
I'm sure it sounds kinda nutty to display 200 columns and 500,000 rows of data. But I have been pulling data from a Lotus Notes database into Sql Server for a while now, but Lotus Notes is...
4
by: ryan | last post by:
Hi Everyone, I'm having trouble convincing myself that Oracle is executing a query of mine as efficiently as possible, and I'm looking for a little guidance. The situation is pretty simple. I...
1
by: david_0 | last post by:
What causes the query optimizer to choose a table spool\lazy spool action in the execution plan? The explanation of "optimize rewinds" makes little sense because my query never comes back to that...
16
by: mamo74 | last post by:
Hello. I am administering a SQL Server (Enterprise Edition on Windows 2003) from some month and can't understand what is going on in the latest week (when the db grow a lot). The DB is around...
6
by: laurenq uantrell | last post by:
Is it possible to use With (NOLOCK) and With (READPAST) in the same SELECT query and what whould be the syntax? @param int SELECT myRow FROM dbo.myTable WITH (NOLOCK) WHERE
7
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL ,...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
5
by: Bruce Pullen | last post by:
DB2 7.2 (7.1.0.68) on AIX 5.2. I have a query that does a table scan on one database, and an index scan on another. The statistics on both databases are up-to-date. Indexes are identical....
6
by: sql_server_user | last post by:
I'm trying to copy all 440 million rows from one table in my SQL Server 2005 db to another table with a different clustering scheme. After a few test inserts that were successful (up to a million...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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,...
0
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...

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.