DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Cloud Database Services Compared: AWS, Microsoft, Google, and Oracle
  • What Developers Need to Know About Table Partition Pruning
  • Rebuild System Databases in SQL Server
  • 10 Database Optimization Best Practices for Web Developers

Trending

  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Streamlining Event Data in Event-Driven Ansible
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Performance Tip for Tuning SQL with UNION

Performance Tip for Tuning SQL with UNION

UNION statements can sometimes introduce performance penalties into your query. This post has a look at how to tune your query up!

By 
Michal Davidek user avatar
Michal Davidek
·
Jul. 27, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
77.3K Views

Join the DZone community and get the full member experience.

Join For Free

This post will be short. It's about a lesson learned by tuning the SQL used for loading items from unrelated tables using the same WHERE clause. It was performed on Oracle 11g, but I am pretty confident that it applies to most SQL databases.

Keynote

Using the "WHERE" clause after the whole "UNION" is performed is significantly slower than using the "WHERE" clause inside inner selects.

This, if you can ensure removal of duplicates by the inner WHERE clauses...

(SELECT * FROM TABLE_1 WHERE COL > 1) a
UNION ALL
(SELECT * FROM TABLE_2 WHERE COL > 1) b;

... is better than following ...

(SELECT * FROM TABLE_1 WHERE COL > 1) a
UNION
(SELECT * FROM TABLE_2 WHERE COL > 1) b;

... which is better than following ...

SELECT * FROM (
(SELECT * FROM TABLE_1) a
UNION
(SELECT * FROM TABLE_2) b)
WHERE COL > 1;

Explanations

  • UNION ALL is faster than UNION because plain UNION is expecting that within two joined datasets are duplicates which need to be removed. If you can ensure (by inner WHERE clauses) that there will be no duplicates, it's far better to use UNION ALL and let database engine optimize the inner selects.
  • Using a WHERE clause on the result of grouped results is too expensive because you are operating on more internal results than you need. Also, the database engine's optimization can’t be processed — the results don't have anything in common.
  • See this.
  • And this.

P.S. If you enjoyed this post, you can share this post anywhere as well as follow me on Twitter to stay in touch.

sql Database engine

Published at DZone with permission of Michal Davidek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cloud Database Services Compared: AWS, Microsoft, Google, and Oracle
  • What Developers Need to Know About Table Partition Pruning
  • Rebuild System Databases in SQL Server
  • 10 Database Optimization Best Practices for Web Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!