Question

Query cost high in oracle

Asked by: skb73

I have this sql ..very simple but it costs very high

select * from table1 where type='abc' and status='1'
it costs 70K
when I change it to
select * from table1 where type='abc' and status='1' and rownum<100 it costs 7k
is this right approach to tackle this issue? or is there a better way?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-03 at 14:40:41ID24869175
Topics

Oracle 8.x

,

Oracle Database

Participating Experts
3
Points
500
Comments
26

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. RowNum
    I want to select 100 rows. What is the syntex of the select query (like rownum<=100 in Oracle)? thank,
  2. RowNum with SQLServer
    Hi, How can I get the 50 to 100 records of a select query (something like "rownum > 50 and rownum < 100" in Oracle)? Thanks
  3. order by and rownum (raking query)
    How I can make that a select returns the first 5 rows of another select that uses the clause order by (the typical ranking). In Oracle8i, I would make: select oficina_trabajo, empleats from (select oficina_trabajo, count(*) empleats from empleados group by oficina...
  4. rownum query in oracle 8.0.5
    I have table tbl_region with follwoing description and 25 records: REG_ID NOT NULL NUMBER(8) REG_DESC NOT NULL VARCHAR2(32) I want to retreive a range of records so that they are ordered in reg_desc asc throughout the range i.e. when the whole data is retreived and wh...
  5. Rownum
    Is there an equivalent of 'rownum' (Oracle) when retrieving data from a table in SQL Server?
  6. Rownum
    select * from table where rownum = 4 no rows. select * from table where rownum <= 4 4 rows returned select * from table where rownum = 1 1 row returned select * from table where rownum = 2 no rows returned. ?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: tangocoderPosted on 2009-11-03 at 15:07:44ID: 25734904

The question about this issue is what level ofcardinality does type and status have.  In other words what do you expect the full number of rows to be collected?  From a quick look at your data it seems that you have 1000 rows being returned.  How long does this query take? If 10% of the rows are being returned the use of a composite index on type and status might have a limited affect on the cost of the query.   These are the issues you should resolve.  Additionally are there alot of updates and deletes?  This will also impact the use of an index on the two columns.

 

by: skb73Posted on 2009-11-03 at 16:33:33ID: 25735392

For 1st one.. here is the explain plan
Plan
SELECT STATEMENT ALL_ROWS Cost: 66,710 Bytes: 27,984 Cardinality: 528
       1 TABLE ACCESS FULL TABLE ORD.ORD_COMPONENT Cost: 66,710 Bytes: 27,984 Cardinality: 528

for 2nd
SELECT STATEMENT ALL_ROWS Cost: 7,552 Bytes: 6,138 Cardinality: 99
       2 COUNT STOPKEY
              1 TABLE ACCESS FULL TABLE ORD.ORD_COMPONENT Cost: 7,552 Bytes: 6,200 Cardinality: 100

 

by: tangocoderPosted on 2009-11-03 at 16:39:33ID: 25735424

How large is the table?  It is collecting 528 rows out of how much?

 

by: it-rexPosted on 2009-11-03 at 17:37:05ID: 25735679

you need to create indexes for columns
type and status;
as you are doing FTS that is why they are expensive.

 

by: sdstuberPosted on 2009-11-03 at 17:45:21ID: 25735704

are your statistics up to date?  do you have any indexes as itrec suggested?  are the index stats up to date?

is status numeric?  if so,  you should use

status=1  

NOT  

status = '1'

only use rownum < 100 if you want to restrict your results to 99 random rows.

 

by: skb73Posted on 2009-11-03 at 19:21:59ID: 25736121

I cannot add indexes to the table since I dont own them .. I can bring down cost only by using hints or tuning queries.. is it feasible.. ?

 

by: sdstuberPosted on 2009-11-04 at 04:33:50ID: 25738640

first, don't tune costs.   lower costs do NOT indicate a better plan, higher costs do NOT indicate a worse plan
tune resource consumption and response time

how long does the query take and how long do you want it to take?
if you trace the execution, what are the wait events?
if your main waits are scattered reads then you most likely will need an index.  either create it your self or have the owner do it.


second, you don't need to be the owner of a table to create an index for it.  You can create an index in your own schema  you just need the "create any index" privilege
if you don't have that privilege then ask your dba to do it for you.

 

by: skb73Posted on 2009-11-04 at 11:26:37ID: 25742902

This is just to correct 800 records.. after that I will not use this query .. so just a simple query with some clause should work. with rownum I got it down to 7k from 70K. is there anything I can add to improve it by a little more?

 

by: sdstuberPosted on 2009-11-04 at 11:30:55ID: 25742948

of course,
you changed the query to do a fraction of the work  and the cost was reduced to a fraction.  Your query was going to modify 800 rows, now it will only modify 99  so the work goes down and the cost reflected that,

don't tune the cost,  it's a made up number.    Note the cost did NOT go down proportionately with the reduction of rows.  

don't tune the cost

small cost != good query
big cost != bad query

 

by: sdstuberPosted on 2009-11-04 at 11:31:48ID: 25742960

if this is a one time effort, then don't bother tuning it.
you are spending more time, effort and money for something that will never be recouped by any efficiency you put into the query.

 

by: skb73Posted on 2009-11-04 at 11:33:39ID: 25742979

my main focus here is the response time .. does small cost mean better response time?
I dont want to use up resources when retrieveing this query.

 

by: sdstuberPosted on 2009-11-04 at 11:39:20ID: 25743029

small cost does NOT mean better response time...

compare these two queries...

on my system

The first has a cost of about 18 quintillion   (1.8x10^19)
the second has a cost of 2

both of these queries return in about 110-120 milliseconds

WITH x AS
    (SELECT 1 FROM
        (SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual),
        (SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual),
        (SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual),
        (SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual UNION ALL SELECT 1 FROM dual)
    )
SELECT ROWNUM FROM x,x,x,x,x,x,x,x,x,x
where rownum < 1000;
 
vs
 
select level from dual connect by level < 1000
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: skb73Posted on 2009-11-04 at 11:42:00ID: 25743061

so to get a good response time , is indexing the only way?

 

by: sdstuberPosted on 2009-11-04 at 11:49:04ID: 25743145

for something as simple as "select * from table1 where type='abc' and status='1'"
you're not going to get a lot of tuning opportunities.

you "might" be able to get better performance with PARALLEL hint.

I see in a post above you said you will be correcting 800 rows.
Does that mean you are iterating over the result set of this select to issue updates?

if so, the performance problem isn't in this query, but the iteration.
try to consolidate the work into a single sql and skip the iteration.

 

by: sdstuberPosted on 2009-11-04 at 11:51:30ID: 25743173

also, just to be clear on my cost example above.

it's somewhat misleading because the second query is in fact better than the first,
but not 9 quintillion times better.

That's the problem with costs, if the value changes a little bit the change doesn't correspond to anything consistently measurable in time or resource consumption.

 

by: skb73Posted on 2009-11-04 at 11:51:45ID: 25743177

no .. getting result set is the issue..it is taking a long time because type or status in the table is not indexed..

 

by: sdstuberPosted on 2009-11-04 at 11:54:29ID: 25743210

also note, indexes won't necessarily help.

if you are querying 800 rows that are spread across most of the data blocks  of your table then an index won't help at all.

what are your wait events?  and how many of them are there?

 

by: skb73Posted on 2009-11-04 at 11:56:26ID: 25743237

How can I find that out?

 

by: sdstuberPosted on 2009-11-04 at 12:00:17ID: 25743285

trace your session  (dbms_monitor)  and then look at your trace file.

or   run this before and after your select and compare the results

SELECT n.name, s.VALUE
FROM v$mystat s, v$statname n
WHERE s.statistic# = n.statistic#

 

by: skb73Posted on 2009-11-04 at 12:14:59ID: 25743443

I am not a dba and I am not even running the other query in prod due to response time ..I am just getting the explain plan in prod

 

by: sdstuberPosted on 2009-11-04 at 12:19:29ID: 25743487

do you have a representative test environment?

what is response time in production?

 

by: skb73Posted on 2009-11-04 at 12:21:43ID: 25743510

we do have a test env that is not even size of the prod. nobody has tried it , they are scared looking at the cost itself to try

 

by: sdstuberPosted on 2009-11-04 at 12:44:05ID: 25743683

a cost of 70K is not that big but again, cost is not a meaningful number

 

by: skb73Posted on 2009-11-05 at 06:26:13ID: 25749747

I tried the query in prod it took 3 min to bring up 800 rows .  I ran the same using java app.it took 43 sec for 10 records .. almost same for 1 record

 

by: sdstuberPosted on 2009-11-05 at 06:30:12ID: 25749778

is table1 actually a table or a view? how big, are your table statistics up to date?
what does the plan actually look like (use dbms_xplan.display)
did you capture your session statistics?

 

by: skb73Posted on 2009-11-19 at 10:45:30ID: 31649698

Thank you for answering my questions.. even though it had high cost it took  2 min to execute Query

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...