Think simply and spare yourself a facepalm

Similar documents
What is Standard APEX? TOOLBOX FLAT DESIGN CARTOON PEOPLE

Powered by. How did trying to give apples away for free change the world?

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

5 R1 The one green in the same place so either of these could be green.

Creating a Brochure in Publisher

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

CIO 24/7 Podcast: Tapping into Accenture s rich content with a new search capability

Introducing the. new & improved. Think. Formerly. Intelligent Business Automation.

Anatomy of a Standard Transcript

Bryan Kreuzberger, Creator of The Breakthrough System Presents. Breakthrough BLUEPRINT

Sucuri Webinar Q&A HOW TO IDENTIFY AND FIX A HACKED WORDPRESS WEBSITE. Ben Martin - Remediation Team Lead

Lecture 1: Overview

Q &A on Entity Relationship Diagrams. What is the Point? 1 Q&A

CIS 45, The Introduction. What is a database? What is data? What is information?

Developers and DBAs. Farmers and City Slickers have different mindsets

Pillar Content & Topic Clusters

Image Credit: Photo by Lukas from Pexels

Appendix: Application of the Formal Principle for the Analysis of Performance Problems After an Oracle Migration

6 Stephanie Well. It s six, because there s six towers.

SYMMETRY v.1 (square)

Topics. CSCI 403 Database Management DISTINCT. JOIN Clause 2/4/2019 DISTINCT JOINS. 12 Miscellaneous Topics

LABORATORY. 16 Databases OBJECTIVE REFERENCES. Write simple SQL queries using the Simple SQL app.

12B. Laboratory. Databases. Objective. References. Write simple SQL queries using the Simple SQL applet.

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9

& ( ); INSERT INTO ( ) SELECT

Oracle SQL. murach s. and PL/SQL TRAINING & REFERENCE. (Chapter 2)

THE MAGIC OF A MILLION DOLLARS BY MAIL!

Are your spreadsheets filled with unnecessary zero s, cluttering your information and making it hard to identify significant results?

EPISODE 23: HOW TO GET STARTED WITH MAILCHIMP

FIVE BEST PRACTICES FOR ENSURING A SUCCESSFUL SQL SERVER MIGRATION

Yammer Product Manager Homework: LinkedІn Endorsements

Sql Server Compare Two Tables To Find Differences

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Top 5 Issues that Cannot be Resolved by DBAs (other than missed bind variables)

MySQL Worst Practices. Introduction. by Jonathan Baldie

mismatch between what is maybe possible today and what is going on in many of today's IDEs.

** Pre-Sell Page Secrets **

How to Read AWStats. Why it s important to know your stats

We re going to start with two.csv files that need to be imported to SQL Lite housing2000.csv and housing2013.csv

1.7 Limit of a Function

Intro. Scheme Basics. scm> 5 5. scm>

Discover How to Watch the Mass Ascension of the Albuquerque International Balloon Fiesta Even if You Can t Be There

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

WELCOME TO OUR PRESENTATION. Erasmus + Project IT CAREER START-UP RO01-KA

Git Workbook. Self-Study Guide to Git. Lorna Mitchell. This book is for sale at

SPRITES Moving Two At the Same Using Game State

5 Templates You Can Use To Get More Product Reviews

Win-Back Campaign- Re-Engagement Series

RESPONSIVE WEB DESIGN IN 24 HOURS, SAMS TEACH YOURSELF BY JENNIFER KYRNIN

How To Create Backlinks

2016 All Rights Reserved

Oracle 9i Application Development and Tuning

Making a PowerPoint Accessible

The Fat-Free Guide to Conversation Tracking

ADO.NET from 3,048 meters

Subversion was not there a minute ago. Then I went through a couple of menus and eventually it showed up. Why is it there sometimes and sometimes not?

Suggested Facebook Privacy Settings

Amber Weyland: [to cameraman] Just hold it there so we can see Mollie.

Assorted Topics Stored Procedures and Triggers Pg 1

_APP A_541_10/31/06. Appendix A. Backing Up Your Project Files

TOP DEVELOPERS MINDSET. All About the 5 Things You Don t Know.

Introduction to Databases

How To Upload Your Newsletter

Magnetize Your. Website. A step-by-step action guide to attracting your perfect clients. Crystal Pina. StreamlineYourMarketing.com

Interpreting Explain Plan Output. John Mullins

The main things to note here are that:

DER GOBBLE. Good Secure Crypto Wallet Practices. What is your wallet?

Introduction to Programming

Making movie magic by Gary Boyle

How To Clone, Backup & Move Your WordPress Blog! Step By Step Guide by Marian Krajcovic

Setting Up Your ios Development Environment. For Mac OS X (Mountain Lion) v1.0. By GoNorthWest. 5 February 2013

A SIMPLE STEP-BY-STEP GUIDE FOR COMPUTER BEGINNERS

Aren t computers wonderful? Well, they are when they work and do what

UPDATING YOUR SCS.1 FIRMWARE

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

Hello, welcome to creating a widget in MyUW. We only have 300 seconds, so let s get going.

ICANN Start -Ep 05 Page 1 of 11. ICANN Start, Episode 5: What Does IPv6 Mean?

Views in SQL Server 2000

Earthquake data in geonet.org.nz

Wow Voic Number Of Rings Iphone 4 >>>CLICK HERE<<<

The complete course: Twitter for mobile apps. Twitter A2Z Part 1. Part 1 setup twitter account and page. April 2014 bonus

TestComplete 3.0 Overview for Non-developers

Introduction to SNNS

Learn a lot beyond the conventional VLOOKUP

SOLUTIONS GUIDE. I Don t Know What to or

How to Get Your Inbox to Zero Every Day

DOWNLOAD PDF LEARN TO USE MICROSOFT ACCESS

MongoDB Schema Design for. David Murphy MongoDB Practice Manager - Percona

Sorting and Filtering Data

WHAT IS SHAREPOINT BRANDING AND UI DESIGN? COPYRIGHTED MATERIAL

Day in the Life of an SAP Consultant using IntelliCorp s LiveCompare Software

How to approach a computational problem

A new clients guide to: Activating a new Studio 3.0 Account Creating a Photo Album Starting a Project Submitting a Project Publishing Tips

THE GOOD, THE BAD AND THE UGLY. How Your Donation Process Impacts Your Workflow (and How To Fix It)

Last, with this edition, you can view and download the complete source for all examples at

Handout 4: Version Control Reference

What Are CSS and DHTML?

Cold, Hard Cache KV? On the implementation and maintenance of caches. who is

The Grab Bag BRAD MILLER, VICE PRESIDENT/CIO (26 YEARS)

Transcription:

Think simply and spare yourself a facepalm Michal Simonik Freelancer Czech Republic Keywords: Oracle; SQL; PL/SQL; Development Introduction Have you ever come up with what you think to be a brilliant solution to a problem? Sure you have, and so have I. But after a brief moment of joy (maybe a day or two if you are lucky, comes that notorious moment of facepalm. Your genius solution crumbles to pieces from a simple comment from your colleague, or from your own discovery after you ve had some rest. Your solution is overly complicated and bad. The correct one is, actually, very simple. In this session, I ll make fun of myself and my mistakes to show listeners how to solve things simply. I ll be presenting examples of simplifying SQL that were given to me for tuning and presenting proper solutions for the problem. In the following section you can find the examples we will go through in our session to find proper solutions. That s a good MERGE, right? My first example is from when I initially learned about MERGE. It came with Oracle 9i, and I loved it. I used it quite often, and I used it in places where it was not good at all. I had to change this exact SQL code two times. The first time was when we were migrating to Oracle 10g, and then again when we were migrating to Oracle 11g. Sometimes it s good to view your SQL like a piece of art. When you look at it, and feel it, you must like it. You must look at it and say, Yeah, I can put this on my wall. Some SQL feel horrible no matter what you do, but that s another story. When you look at this SQL, it does not FEEL right and that can be said about every SQL you ll see in our session. So use your inner force and tell me what you don t like about this particular query: MERGE INTO metadata_tab met USING ( SELECT DISTINCT zp, obd_from_rb, obd_to_rb, obd_from_rz, obd_to_rz fakta_tab WHERE module_code = 'P304' AND period = 201209 fak ON ( NVL(fak.zp, 0 = NVL(met.zp, 0

AND NVL(fak.obd_from_rb, 0 = NVL(met.obd_from_rb, 0 AND NVL(fak.obd_to_rb, 0 = NVL(met.obd_to_rb, 0 AND NVL(fak.obd_from_rz, 0 = NVL(met.obd_from_rz, 0 AND NVL(fak.obd_to_rz, 0 = NVL(met.obd_to_rz, 0 WHEN NOT MATCHED THEN INSERT ( id_meta_ident, zp, obd_from_rb, obd_to_rb, obd_from_rz, obd_to_rz VALUES ( metadata_seq.nextval+100, fak.zp, fak.obd_od_rb, fak.obd_do_rb, fak.obd_od_rz, fak.obd_do_rz ; SELECT from SELECT from SELECT and from SELECT This horror story of SQL is actually not mine. It s from an employee of an unknown company. The listed query is from a validation module. Its purpose is to identify multiplicities in medical care data based on patient ID and the dates when medical care was provided, and then present them in tables like the following: ID Patient ID Date ID of duplicate 1 FWER241 1.1.2015 5 1 FWER241 1.1.2015 11 1 FWER241 1.1.2015 23 2 33GSW52 4.5.2015 632 2 33GSW52 4.5.2015 745 And, thus, was the underlying code born (table patient_data has millions of rows: SELECT id_uni, patient_id_uni, procedure_date_uni, id_dpl (SELECT uni.procedure_date procedure_date_uni, uni.patient_id patient_id_uni, uni.id id_uni, dpl.procedure_date procedure_date_dpl, dpl.patient_id patient_id_dpl, dpl.id id_dpl (SELECT master.* patient_data master

WHERE EXISTS (SELECT 1 (SELECT * patient_data source WHERE master.procedure_date = source.procedure_date AND master.patient_id = source.patient_id AND master.rowid > source.rowid dpl, (SELECT * patient_data WHERE ROWID IN (SELECT ID_ROW (SELECT MIN(ROWID AS ID_ROW, procedure_date, patient_id patient_data GROUP BY procedure_date, patient_id uni WHERE dpl.procedure_date = uni.procedure_date AND dpl.patient_id = uni.patient_id unirowid, patient_data master_data WHERE unirowid.patient_id_uni = master_data.patient_id AND unirowid.procedure_date_uni = master_data.procedure_date AND unirowid.id_uni = master_data.id ORDER BY unirowid.patient_id_uni, unirowid.procedure_date_uni, unirowid.id_dpl; This tricky problem can be solved with quite a simple and much faster piece of SQL. Let s see if we can figure it out Copy and paste INSERT The following example is quite common. We have one set of data which we have to insert into multiple tables in certain forms. My only excuse for this extract is that it s from Oracle 8i, and I was very young at the time, not knowing what SQL and PL/SQL context were. But I did know that there was something like an execution plan! PROCEDURE seq NUMBER; BEGIN INSERT INTO dm_owner.ms_cs2vd501011_tmp (SELECT * csu2011.cs2vd501011@vm15dbv_ora112i WHERE stat_date = p_stat_date_in;

FOR sel IN (SELECT * dm_owner.cs2vd501011_tmp LOOP SELECT dmv_s_statobj.nextval INTO seq dual; INSERT INTO dmv_t_statobj (id_statobj,, module VALUES (seq,, p_module_in; VALUES ('ISEKTOR',,seq, 1; VALUES ('FORMA',,seq, 1; VALUES ('KATP',,seq, 1; VALUES ('MNACE',,seq, 1; VALUES ('POCP',,seq, 1; END LOOP; END; I bet you can do better! I have to SELECT to see! Our second to last real-life example is quite simple. We have an online shop, and every order consists of order items. The goal of the following procedure is to delete items from a particular order and write deleted items to output. Not only can the following code be written faster, there is also one nasty flaw in it. Can you find it? PROCEDURE delete_order (p_id orders.id%type IS BEGIN dbms_output.put_line('deleting Order ID: ' p_id; FOR l_idx IN (SELECT * mtg.order_items WHERE order_id = p_id LOOP dbms_output.put_line('deleting Item ID: ' l_idx.id; END LOOP; DELETE mtg.order_items WHERE order_id = p_id; END; Ultimate facepalm After one of my training sessions I was presented with a simple query which looked like this. SELECT company, COUNT(* invoices WHERE can_access( company = 1 GROUP BY company;

The function can_access contained a single SQL and some simple PL/SQL code. The purpose of this function was to filter records based on the privileges of the current user. First, I told them that I don t like the usage of a PL/SQL function at all in this kind of situation. It s a CPU burner on SQL and PL/SQL context switches in the first place, and second, it s hiding some important information from Oracle optimizer (selectivity for example. The response was that this was legacy stuff and that they had to deal with it somehow ouch. The problem was that they knew that function could be run on a grouped result set (limiting calls a great deal, but they was unable to force Oracle to do so. Want to hear what I proposed after a long day? It was not very good. Contact address: Michal Simonik Charbulova 23 618 00, Brno Czech Republic Phone: +42(0730-182935 Email misimonik@gmail.com Internet: www.michalsimonik.com LinkedIn: https://cz.linkedin.com/in/michalsimonik Twitter: @MichalSimonik