Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:37:09+00:00 2026-05-26T02:37:09+00:00

I wonder if using CASE … WHEN … THEN expression in MySQL queries has

  • 0

I wonder if using CASE … WHEN … THEN expression in MySQL queries
has negative effect on performance?

Instead of using CASE expression (for example inside your UPDATE query)
you always have possibility to make if else statement in your program
written in php, python, perl, java, … to choose wich query to send, for example (in pseudocode):

prepareStatement(
"UPDATE t1 SET c1=c1+1, msg=CASE (@v:=?) WHEN '' THEN msg ELSE @v END"
);
setStatementParameter(1, message);

or insead:

if (message == "") {
    prepareStatement("UPDATE t1 SET c1=c1+1");
} else {
    prepareStatement("UPDATE t1 SET c1=c1+1, msg=?");
    setStatementParameter(1, message);
}

(c1 here needed just to show that something happens in both cases)

What way of doing it has better performance?
And how much the performance penalty is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:37:10+00:00Added an answer on May 26, 2026 at 2:37 am

    Pretty much all per-row functions will have an impact on performance, the only real question is: “Is the impact small enough to not worry about?”.

    This is something you should discover by measuring rather than guessing. Database administration is only a set-and-forget activity if neither your data nor your queries ever change. Otherwise, you should be periodically monitoring performance to ensure no problems occur.

    By “small enough” in the above comments, I mean, you probably needn’t worry about the performance impact of something like:

    select * from friends where lowercase(lastname) = "smith"
    

    if you only have three friends.

    The impact of these things becomes more serious as the table increases in size. For example, if you have one hundred million customers and you want to find all the ones likely to be computer-related, you wouldn’t want to try:

    select name from customers where lowercase(name) like '%comp%'
    

    That’s likely to bring your DBAs down on you like a ton of bricks.

    One way we’ve fixed this in the past is to introduce redundancy into the data. Using that first example, we would add an extra column called lowerlastname and populate it with the lowercase value of lastname. Then index that for search purposes and your select statements become blindingly fast, as they should be.

    And what does that do to our much loved 3NF, I hear you ask? The answer is “not much”, if you know what you’re doing 🙂

    You can set up the database so that this new column is populated by an insert/update trigger, to maintain data consistency. It’s perfectly acceptable to break 3NF for performance reasons, provided you understand and mitigate the consequences.

    Similarly, that second query could have an insert/update trigger that populated a new indexed column name_contains_comp whenever an entry was updated or inserted that contained the relevant text.

    Since most databases are read far more often than they’re written, this moves the cost of the calculation to the insert/update, effective amortising it across all select operations. The query would then be:

    select name from customers where name_contains_comp = 'Y'
    

    Again, you’ll find the query blindingly fast at the minor cost of slightly slower inserts and updates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder is there any positive effect in using UNSIGNED flag on defining some
I wonder if anyone tried using Kaazing for client push with Silverlight? How did
I wonder if I can test landscape view using simulator?
I wonder if there is a way to either programatically or using a third
i wonder if it is possible to cascade converters when using wpf databinding. e.g.
I wonder if is possible to use FTS with LINQ using .NET Framework 3.5.
I am using jQuery to make some ajax calls and wonder how people handle
i'm using spring+tapestry for authenticate webuser. I wonder is there any technique i can
I am using a DBGrid component in Delphi. I wonder how I can set
I wonder if someone can help: Long story short, I'm using MSSQL2005 to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.