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;
}
}