Reputation: 12488
I have a simple query in DQL that selects 3 columns:
$qb->select("c.zip_code, c.name, s.state")
->where("c.zip_code LIKE :input");
My desired output is
"[c.zip_code], [c.name], [s.state]"
Currently, I'm concatenating strings on the PHP level to get the desired string.
Is it faster to do it on the database level by using the CONCAT() function?
Upvotes: 4
Views: 2043
Reputation: 1
You can scale out PHP servers more easily than MySql servers. For example on AWS you can create autoscaling conditions that will scale your PHP server instances up/down.
Upvotes: -1
Reputation: 53830
Given two somewhat equal choices, which I believe you have in this case, I will use the risk factor to make the final decision.
If I later need to split the columns apart in the application, it will be costly, if not impossible to split the columns in the application after they've been joined by the database. Therefore, there is less risk in joining them in the application, where I have the data needed to present them either way.
Also, I feel that scaling the application layer is easier than scaling the database layer. You simply add application servers. A database bottleneck is more difficult to handle than an application bottleneck.
Upvotes: 0
Reputation: 3523
I would let the database do the heavy lifting and concatenate the data, while it may not mean much for smaller applications, for larger applications your application script might start hitting memory thresholds.
What happens if you need to show the same data in a different place, do you copy and paste the code or use the same query?
Bottom line, the performance may be the same now, but later issues like code reuse, ease of changes become more important.
Upvotes: 1
Reputation: 1161
I want to go with mysql level why we have to take care of this manipulation where mysql gives in built function for this my opinion MYSQL and both take probably same time
Upvotes: 1
Reputation: 53533
This sort of logic belongs in your application's display layer, not the database.
Upvotes: 3
Reputation: 237
This isn't really a point on which optimisation is relevant. You should be conserned with which one is more readable/practical, personally I would do it on PHP level.
Upvotes: 6