Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming pattern for filtering SQL database contents in several different ways
    primarykey
    data
    text
    <p>I'm working on a project where we have a table in a SQL database which will potentially contain a relatively large number (hundreds of thousands) of rows.</p> <p>This project is written in PHP, and is using PDO to access my database. In this project, to implement "reports" and various things, I need to be able to select rows out of this table, filtering the data by different properties, depending on what context I'm going to be displaying the data in.</p> <p>I'm curious what a very well-performing design for this type of situation would be. I've thought of the following options for this, but I don't really like any of them:</p> <ul> <li>Write a separate query (i.e. API function) for each different filtering technique. This seems undesirable because I'll be duplicating 90% of the SQL in each function, and it's hard to maintain (e.g., if I need to alter / fix one of my queries, I now need to alter / fix ALL of them, instead of just one).</li> <li>Provide a single API function which retrieves an unfiltered list of rows, and then filter them in my application, instead of using SQL. This is undesirable because it's wasting a lot of effort processing rows my application doesn't care about.</li> <li>Write a single function with a single query, with various optional parameters which, if provided would alter the <code>WHERE</code> clause of the query. This is probably the best option I have so far, but I'm still not sure I like it because it would mean having a function with a really weird signature, and having a bunch of ternary statements to alter the <code>WHERE</code> clause depending on whether or not the arguments were given.</li> </ul> <p>Are there one or more generally accepted programming patterns for designing an API which provides this type of functionality?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload