Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      A Breeze Of Inspiration In September (2025 Wallpapers Edition)

      August 31, 2025

      10 Top Generative AI Development Companies for Enterprise Node.js Projects

      August 30, 2025

      Prompting Is A Design Act: How To Brief, Guide And Iterate With AI

      August 29, 2025

      Best React.js Development Services in 2025: Features, Benefits & What to Look For

      August 29, 2025

      Report: Samsung’s tri-fold phone, XR headset, and AI smart glasses to be revealed at Sep 29 Unpacked event

      September 1, 2025

      Are smart glasses with built-in hearing aids viable? My verdict after months of testing

      September 1, 2025

      These 7 smart plug hacks that saved me time, money, and energy (and how I set them up)

      September 1, 2025

      Amazon will sell you the iPhone 16 Pro for $250 off right now – how the deal works

      September 1, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Fake News Detection using Python Machine Learning (ML)

      September 1, 2025
      Recent

      Fake News Detection using Python Machine Learning (ML)

      September 1, 2025

      Common FP – A New JS Utility Lib

      August 31, 2025

      Call for Speakers – JS Conf Armenia 2025

      August 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Chrome on Windows 11 FINALLY Gets Touch Drag and Drop, Matching Native Apps

      August 31, 2025
      Recent

      Chrome on Windows 11 FINALLY Gets Touch Drag and Drop, Matching Native Apps

      August 31, 2025

      Fox Sports not Working: 7 Quick Fixes to Stream Again

      August 31, 2025

      Capital One Zelle not Working: 7 Fast Fixes

      August 31, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Just a Few Updates

    CodeSOD: Just a Few Updates

    July 17, 2025

    Misha has a co-worker who has unusual ideas about how database performance works. This co-worker, Ted, has a vague understanding that a SQL query optimizer will attempt to find the best execution path for a given query. Unfortunately, Ted has just enough knowledge to be dangerous; he believes that the job of a developer is to write SQL queries that will “trick” the optimizer into doing an even better job, somehow.

    This means that Ted loves subqueries.

    For example, let’s say you had a table called tbl_updater, which is used to store pending changes for a batch operation that will later get applied. Each change in updater has a unique change key that identifies it. For reasons best not looked into too deeply, at some point in the lifecycle of a record in this table, the application needs to null out several key fields based on the change value.

    If you or I were writing this, we might do something like this:

    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> id <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span>, <span class="hljs-type">date</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span>, location <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span>, type <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span>, type_id <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span>
    <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>
    

    And this is how you know that you and I are fools, because we didn’t use a single subquery.

    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> id <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
    
    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> <span class="hljs-type">date</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
    
    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> location <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
           
    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> type <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
           
    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> <span class="hljs-type">date</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
           
    <span class="hljs-keyword">update</span> tbl_updater <span class="hljs-keyword">set</span> type_id <span class="hljs-operator">=</span> <span class="hljs-keyword">null</span> <span class="hljs-keyword">where</span> updater <span class="hljs-keyword">in</span>
            (<span class="hljs-keyword">select</span> updater <span class="hljs-keyword">from</span> tbl_updater <span class="hljs-keyword">where</span> change <span class="hljs-operator">=</span> <span class="hljs-variable">@change</span>)
    

    So here, Ted uses where updater in (subquery) which is certainly annoying and awkward, given that we know that change is a unique key. Maybe Ted didn’t know that? Of course, one of the great powers of relational databases is that they offer data dictionaries so you can review the structure of tables before writing queries, so it’s very easy to find out that the key is unique.

    But that simple ignorance doesn’t explain why Ted broke it out into multiple updates. If insanity is doing the same thing again and again expecting different results, what does it mean when you actually do get different results but also could have just done all this once?

    Misha asked Ted why he took this approach. “It’s faster,” he replied. When Misha showed benchmarks that proved it emphatically wasn’t faster, he just shook his head. “It’s still faster this way.”

    Faster than what? Misha wondered.

    [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWhatSie – WhatsApp web client
    Next Article Plasma Bigscreen: L’interfaccia TV basata su KDE ritorna con nuove funzionalità

    Related Posts

    News & Updates

    Report: Samsung’s tri-fold phone, XR headset, and AI smart glasses to be revealed at Sep 29 Unpacked event

    September 1, 2025
    News & Updates

    Are smart glasses with built-in hearing aids viable? My verdict after months of testing

    September 1, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Two revolutionary 90s shooters shadow dropped on Xbox, Xbox Game Pass, and PC — Complete with enhanced performances, online multiplayer crossplay, and more

    News & Updates

    CVE-2025-54066 – DiracX-Web Redirect Hijacking Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Top 5 desktop PC case features that I can’t live without — and neither should you

    News & Updates

    New ‘CitrixBleed 2’ NetScaler flaw let hackers hijack sessions

    Security

    Highlights

    Team Group’s New P250Q SSD Can Erase Itself, Built for Military and Industrial Use

    July 14, 2025

    If you’re working in a high-stakes environment where sensitive data must never fall into the…

    Last week in AI dev tools: Cloudflare blocking AI crawlers by default, Perplexity Max subscription, and more (July 7, 2025)

    July 7, 2025

    Third Time in 90 Days: Louis Vuitton UK Latest Victim in LVMH Cyberattacks

    July 15, 2025

    ROG Xbox Ally & Ally X Finally Get Official Release Date

    August 20, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.