简体   繁体   中英

Storing MySQL queries in files

I had developed method to read complex or lengthy queries from a file instead of directly in code to avoid the clutter and provide quick references to the queries themselves. I was then told this was not preferred since the script had to read from a file and was thereby slower. Here's my example:

file: sql/get_code_matches.sql

/**
* returns list of new codes
* from old code entry
* @param source - string oldcode
* @param startdate - YYYY-MM-DD format only
* @return mixed multi-dimensional array on success; false on no record
* @see @DB::is_error() in code for thrown exceptions
*
*/
SELECT NULL,
       g.source,
       g.target,
       dx9.NAME AS oldcode,
       dx9.DESCRIPTION old_description,
       dx10.NAME AS newname,
       dx10.DESCRIPTION AS new_description,
       g.flags,
       SUBSTR(g.flags, 4, 1) AS scenario,
       SUBSTR(g.flags, 5, 1) AS POSITION,
       SUBSTR(g.flags, 1, 1) AS approximate,
       SUBSTR(g.flags, 3, 1) AS combination,
       SUBSTR(g.flags, 2, 1) AS matches,
       CASE
           WHEN SUBSTR(g.flags, 2, 1) = 1 THEN 'No Matches'
           ELSE 'Matches'
       END AS MATCH TYPE,
                    CASE
                        WHEN SUBSTR(g.flags, 3, 1) = 1 THEN 'Combination Matches'
                        ELSE CASE
                                 WHEN SUBSTR(g.flags, 1, 1) THEN 'Approximate Matches'
                                 ELSE 'Match'
                             END
                    END AS status
FROM tablex g
LEFT JOIN tabley dx10 ON dx10.CODE = g.target
LEFT JOIN tablez dx9 ON dx9.CODE = g.source
WHERE 1
  AND g.source = ?
  AND g.startdate = ?

// then somewhere in the OOP code:

$sql = $this->db->load_sql('get_code_matches'); // reads sql from file
$results = $this->db->FetchAll($sql, $bindParams); // returns results via pdo object

The sql folder is protected via .htaccess, and data is obtained for a custom db layer.

We found that it is more organized in our model area to label complex queries such as these.

The queries are only loaded into the code when they are needed.

Depending on the IDE, the 'sql' folder is always open for quick reference. In some cases, the sql folder may contain sub-folders represent each module of the project.

Would this be considered bad practice ?

For the 99.99% of sites it wouldn't be of any significance.

For the 0.01% high load sites it would be advised to put these SQL queries into PHP files, to let them be cached in opcache and thus not read on each request.

I think it's a very good idea to store these MySQL queries in separate files. I'm not sure how you're including the files, but the performance hit is probably negligible. Even if this makes the script marginally slower, I think it's worth it to have more maintainable code.

I often see people doing various micro optimizations like this (just look at the comments on is_null($a) vs $a === null ), and it's never worth it. There are lots of things to optimize that actually matter, but this is not one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM