c - What is the recommended way to write this sql statement? -


Considering both readability and performance, what is the best way to format the following sql statement. Thank you

  sql = (char *) "select * from tblEvent_basic, rowid" "where" "(" "(start_time & gt" service_id = else? "; = And Start_time & lt; =?) or "" (END_TIME & gt ;? and END_TIME & lt ;?) or "" (start_time & lt ;? and END_TIME & gt ;?) "") "order by" "Start_time ASC;  

Edit: 1.sqlite3 database engine; Using CIP; Running on MIPS24K 250M embedded CPU

2. Second, fourth, 6th parameter is same, and 3, 5, 7 is the same.

  RC = Sqlite3_bind_int (sql_stmt, 1, service_id); Rc = sqlite3_bind_text (sql_stmt, 2, ts.start, 5, SQLITE_TRANSIENT); Rc = sqlite3_bind_text (sql_stmt, 3, ts.end, 5, SQLITE_TRANSIENT); Rc = sqlite3_bind_text (sql_stmt, 4, ts.start, 5, SQLITE_TRANSIENT); Rc = sqlite3_bind_text (sql_stmt, 5, ts.end, 5, SQLITE_TRANSIENT); Rc = sqlite3_bind_text (sql_stmt, 6, ts.start, 5, SQLITE_TRANSIENT); Rc = sqlite3_bind_text (sql_stmt, 7, ts.end, 5, SQLITE_TRANSIENT);  

Your temporary status is currently:

  "(start_time & gt; = and start_time & LT ;? =) or" "(end_time & gt; and END_TIME the & lt;?) or" "(start_time & LT ;? and END_TIME & gt;)" < / Code> 

We can improve readability (continuous width fonts) with some locations immediately:

  "(start_time & gt; =? And start_time & lt ; =?) or "" (END_TIME & gt;? and END_TIME & LT ;?) or "" (start_time & LT ;? and END_TIME & gt;) " 
and commentary , We know that the same value will be given to the placeholder 1, 3, 5, and one The different values ​​will be passed to Placeholder 2, 4, 6 (but they all get the same value). In addition, if we call them t1 and t2 times, we can assume that t1 < = T2 .

So, what is this criterion?

  • The start time limit comes in TT .. T2
  • The expiration time range comes in T1. T2
  • Start time T1 is before and end time is more than T2

This is an overlap criterion that is written in a difficult manner - it should be replaced:

< pre> "(start_time & LT ;? = and END_TIME & gt; =?)"

that placeholder here t2 and placeholder eats two mail To match except t1 . If you do not want such incidents that are counted to meet the deadline (i.e., you do not want to count the events ending immediately on t1 , or the event that started immediately T2 ), then ' & gt; = 'and' & lt; = 'in' & gt; Change and ' & lt; '.

This overlap is a standard way of writing write, when the end times are also included. The situation is very simple - no or conditions - and reliable. It will be faster that the adapter has less work, and possibly that the performance engine will have lower criteria for applying. (Actually good optimizer can see the equivalence of 2-placeholder and 6-placeholder versions, but I do not want to bet on doing this - at least not because the optimizer can not tell that placeholders 1,3,5 same , Nor will placeholders 2,4,6 will be identical, it can only be determined when the statement is executed, then it may be difficult to leak again.)


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -