Advertisement

05.16.2008 at 07:28AM PDT, ID: 23408380 | Points: 125
[x]
Attachment Details

Sql query

I have a very basic question. When you have the following query in Oracle10gr2, do we need to have index on table a for rid and bid to perform better? I know when you have a where clause based on rid and bid the index helps, but your query joins those tables to get c2 and c3 in the select clause. ( these bid and rid are foreign keys on table a and table a contains around 100 million rows)

select a.c1,r.c2,b.c3
from a
   left join r
       on a.rid = r.rid
   left join b
      on a.bid = b.bid
where a.cid between :v1 and :v2
Start your free trial to view this solution
Question Stats
Zone: Database
Question Asked By: tvgkarthik
Question Asked On: 05.16.2008
Participating Experts: 2
Points: 125
Views: 0
Translate:
Loading Advertisement...
05.16.2008 at 07:44AM PDT, ID: 21583172

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 07:46AM PDT, ID: 21583191

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 08:20AM PDT, ID: 21583514

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 08:30AM PDT, ID: 21583632

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 10:03AM PDT, ID: 21584433

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 10:43AM PDT, ID: 21584850

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.16.2008 at 07:44AM PDT, ID: 21583172
It depends on:
- How many rows exist in tables r and b
- How selective the data is in these tables
- How many rows will be retrieved from table a i.e. identified by the where condition

It is common practise to create indexes on foreigh key columns as this usual has a positive impact on performance when deleting data from parent tables i.e. if you want to delete a row from table r, then assuming you have a foreign key check constraint on table a, a lookup will be performed on a to ensure no data exists, or to delete it if the cascade delete options was used.  There are also locking implications for using indexes on foreign keys.
 
05.16.2008 at 07:46AM PDT, ID: 21583191
....should have added of course that there there may also be a down side to having indexes on tables with many rows - the index maintenance overhead due to inserting all these rows!
 
05.16.2008 at 08:20AM PDT, ID: 21583514
THis is dataware house environment. I might have 15 to 16 dimension keys on the fact table a. When I  am not using a where clause and using the above query, do you think I still need to have all foreign key indexes, you know the impact on the load  and space due to these indexes.
 
05.16.2008 at 08:30AM PDT, ID: 21583632
Actually, no. It is unnecessary to index a.rid or a.bid.

You are doing 2 left joins on the table a, which means that the query will run through ALL rows in a which match the where clause. It will then take each a.rid value and try to match it with rows from table r, and similarly will match a.bid with rows from table b.

It is essential that r.rid and b.bid are indexed. Presumably these are primary keys so will be indexed.

BTW if the joins were inner joins (so that only matching rows of table a are returned) it is possible that indexing a.rid and a.bid might help.
 
05.16.2008 at 10:03AM PDT, ID: 21584433
Table a will have matching rows of rid/bid but it can also have null values, that is why we had to use left join. If I join thru inner join are you sure that index will help to perform the query better though I don't use these rid and bid in my where clause.

 What I mean is  if I perform the following query, any way it needs to bring all the records from table a for a given cid because there is no where condition on bid and rid. So left join or inner join should not make much difference. But if you think indexes with inner join performance will be better, then I can think of adding a bid and rid values as 0 and add 0 to the rows in table a where these bid and rid (foreign keys are nulls).  This is a huge change to our system. Before I make this decission it is very important for me be clear on this.
select a.c1,r.c2,b.c3
from a
    join r
       on a.rid = r.rid
   join b
      on a.bid = b.bid
where a.cid between :v1 and :v2
 
05.16.2008 at 10:43AM PDT, ID: 21584850
If you want to return all rows of table a which match the where condition, you should use a left join. Then, as I said, you won't need indexes on a.rid and a.bid.

However, if you only want those rows of table a which match the where condition AND which have corresponding rows in tables r and b, then you should use an inner join. Note that even though the conditions a.rid = r.rid and a.bid = b.bid are not part of the where clause, for an inner join they are treated as if they were part of the where clause.

You should use whichever of these gives you the data you are looking for.

If you want to achieve the effect of the left join by "cheating" - adding dummy values to the other tables and using an inner join - I would say, don't. I can't imagine it would run any faster and you would have added spurious data which might cause problems somewhere else.

For any given SQL query, the database engine tries to define an optimum execution plan - which table it looks at first, how it matches rows together and so on. If the execution plan was to start with table b, say, and for each row in b look for a row in table a and then check whether the where clause was satisfied, then an index on a.bid would improve the performance. However, the database would choose this execution plan only (a) if it was an inner join (b) if table b was much shorter than table a and (c) if the where clause was not very selective (in other words if a high percentage of rows in table a match the where condition).

In the case you describe - you do want all the rows in table matching the where condition - just use the left join and forget anything else.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628