-1

I have a database with very large number of records. More specifically I have a query with multiple sub-queries in it which fetch records and display on front end.

I want to show some default value if field contains NULL.

I wish to find out which of the below approach can give better performance. I am using MySQL approach for now, but it takes lot of time.

MySQL Approach

SELECT A.columnA,
IFNULL (( SELECT B.columnB FROM tableB B), 'default-value') AS columnB,
IFNULL (( SELECT C.columnC FROM tableC C), 'default-value') AS columnC,
IFNULL (( SELECT D.columnD FROM tableD D), 'default-value') AS columnD
FROM tableA A
WHERE 1=1
LIMIT 20
ORDER BY A.columnA ASC

PHP Approach

foreach($recordsFromDB as $record) {
    if(is_null($record->columnB)) {
        echo 'default-value';
    } else {
        echo $record->columnA;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Irfan Ahmed
  • 9,136
  • 8
  • 33
  • 54

1 Answers1

0

There isn't a perfect way to do something in programming, therefore alot of solutions were found! so let's start from this point, in my opinion, checking for null values should be using PHP, and then the handles you've gotten can be passed for further MySQL processing! You obviously will have tough time processing all this amount of queries using just MySQL. My advice, try checking null values and replacing them in php and if you had to deal with retrieving/fetching or inserting values in database hit MySQL

Isaac
  • 89
  • 7