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

      Error’d: Pickup Sticklers

      September 27, 2025

      From Prompt To Partner: Designing Your Custom AI Assistant

      September 27, 2025

      Microsoft unveils reimagined Marketplace for cloud solutions, AI apps, and more

      September 27, 2025

      Design Dialects: Breaking the Rules, Not the System

      September 27, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025

      Cailabs secures €57M to accelerate growth and industrial scale-up

      September 12, 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

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025
      Recent

      Using phpinfo() to Debug Common and Not-so-Common PHP Errors and Warnings

      September 28, 2025

      Mastering PHP File Uploads: A Guide to php.ini Settings and Code Examples

      September 28, 2025

      The first browser with JavaScript landed 30 years ago

      September 27, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured
      Recent
    • 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 ArticleNew Konfety Malware Variant Evades Detection by Manipulating APKs and Dynamic Code
    Next Article Designing Momentum: The Story Behind Meet Your Legend

    Related Posts

    News & Updates

    Building personal apps with open source and AI

    September 12, 2025
    News & Updates

    What Can We Actually Do With corner-shape?

    September 12, 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

    Jumbo-sized JavaScript for issue 747

    Development

    You can delete sent text messages on Android now – here’s how

    News & Updates

    The best CRM software with email marketing in 2025: Expert tested and reviewed

    News & Updates

    Glassmorphism CSS Generator

    Web Development

    Highlights

    Genie Envisioner: A Unified Video-Generative Platform for Scalable, Instruction-Driven Robotic Manipulation

    August 11, 2025

    Embodied AI agents that can perceive, think, and act in the real world mark a…

    Don’t Just Follow Tips — Dhanarthi Shows You How to Trust Your Own Analysis

    July 26, 2025

    Free decryptor for victims of Phobos ransomware released

    July 24, 2025

    Smashing Security podcast #432: Oops! I auto-filled my password into a cookie banner

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

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