Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Which is better / faster, RAW sql or ORM / PHQL?

Hi...

I am a beginner here. I have a question here:

  1. If I decide to use only mysql, what if I just skip ORM and use RAW SQL query ?
  2. What is advantage, disadvantage using RAW SQL ?
  3. Which one is perform better, ORM / PHQL / RAW SQL

Thanks

Robby



47.6k
Accepted
answer
edited Apr '15

With raw SQL, the code that interacts with the database will run faster as there are less abstraction layers to go through. However, since those abstraction layers are built in compiled C, rather than PHP, the actual difference in execution time is negligible. What you will be losing is the simple way the ORM allows you to run queries without hand coding each query.

I'm a big proponent of efficiency, but I haven't noticed any slowdown by using ORM/PHQL, and my development time is much, much lower because I can do stuff like this:

$Applicants = $Program->getApplicants()

Instead of

$Applicants = [];
$program_id = 3;
$query = "SELECT * FROM `applicants` WHERE program_id = $program_id";
$result = mysql_execute($mysqli_connection,$query);
while($row = mysqli_fetch_assoc($result)){
    $Applicant = new Applicant();
    $Applicant->program_id = $program_id;
    $Applicant->name = $row['name'];
    // and so on for every attribute