0

I am working on something and found this SQL that I am not sure how to rewrite correctly, meaning using PHP PDO.

The SQL looks like:

$sql = 'SELECT * FROM table WHERE column ' . isset($variable) ? . '=' . $variable : '>0'; 

Basically what the query is telling is: if $variable is defined (within the PHP world) then use an = for the WHERE condition, if it is not then use the >0.

I can clean up that a little bit on PHP and do something like:

$where = $variable ? 'column = ?' : column > ?'; // ternary operator to build the proper where condition $sql = 'SELECT * FROM table WHERE $where'; $db->row($sql, [$variable ?? 0]); // bind parameters to the query, PDO way, and the operator will use the value of $variable if it is defined otherwise it will use 0 

and it will work fine, I guess. Now, I wonder if I can achieve the same using plain SQL like a condition inside the WHERE same as within the SELECT, and if so, is it optimum? Or programmatically is better and faster?

1 Answer 1

1

Now, I wonder if I can achieve the same using plain SQL like a condition inside the WHERE same as within the SELECT

Yes.

and if so, is it optimum?

Nope.

Or programmatically is better and faster?

Yup.

Each expression in a WHERE clause is called a predicate. The more complex that expression is, the harder it is for the SQL engine to optimize for. Eventually it can hurt the sargability of the predicate. Sargability is the quality of how likely a predicate is applicable for an efficient index seek operation. A predicate that isn't sargable will likely result in a less performant operation to serve the data for the query.

Doing the more complex logic on the application side to construct a simpler predicate is typically better and more efficient, when possible.

As discussed in the comments, this would be the pseudo-code for how one could logically construct a similar predicate in pure SQL:

DECLARE @Variable INT = NULL; SELECT * FROM table WHERE ( @Variable IS NOT NULL -- Only if the variable has a value is when it'll be used to filter on AND column = @Variable ) OR column >= 0; -- Otherwise we fallback onto our default filter 

The OR operator is what essentially hurts the sargability here. One could re-write this more efficiently in pure SQL using a UNION operator instead like so:

DECLARE @Variable INT = NULL; SELECT * FROM table WHERE @Variable IS NOT NULL -- Only if the variable has a value is when it'll be used to filter on AND column = @Variable UNION ALL SELECT * FROM table WHERE @Variable IS NULL AND column >= 0; -- Otherwise we fallback onto our default filter 

At first glance, this may seem counterintuitive to query the same table twice, but the engine is typically smart enough to eliminate what doesn't apply. E.g. in this case, either the variable has a value or it doesn't, so only one side of the UNION ALL operator is processed.

FWIW, this pseudo-code is actually valid T-SQL syntax for Microsoft SQL Server (should you ever end up working in that system).

5
  • Can you add an example of the correct SQL with the condition in the WHERE clause, just for future reference? Commented Dec 27, 2023 at 14:16
  • @ReynierPM I'm not as familiar with MySQL syntax around variables, would it suffice if I provided pseudo-code? Also, please see my updated answer with more explanation. Commented Dec 27, 2023 at 14:17
  • 1
    yeah, no worries about it be in MySQL syntax just a general idea would be enough and FYI this is just to increase my culture and knowledge as you said I was in the right track already :) Commented Dec 27, 2023 at 14:19
  • 1
    @ReynierPM Please see my updated answer with example pseudo-code. Commented Dec 27, 2023 at 14:23
  • @ReynierPM For brownie points, I added one last update with another example code on how to re-write this particular case in pure SQL in a more efficient way than the previous example. But the general principle of simplifying the predicate by constructing the expression in application code still holds true, most times. Commented Dec 27, 2023 at 14:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.