Prepared V Inline sql
Posted in 1996
Topics: Performance & Tuning, Connectivity: ESQL/C, 4GL & Embedded SQL
The environment is Online V5 under AIX. We have been running some simlple tests to compare the performance of prepared sql to inline coded sql in an esql-C program. Our intuition and experience of other databases tells us that prepared (dynamic) sql _should_ be slower than inline coded (pre-compiled) sql since the prepared sql will require parsing each time it is called whilst the inline code will have all references and pointers resolved at compile & link time. Not so! Consider the following (pseudo-)code fragment (assume that all variables and tables are declared correctly):- --- EXEC SQL PREPARE up1 FROM "INSERT INTO temp1 VALUES (?); print time; /* prepared loop */ for (i=1 ; i<10000 ; i++) { temp1_int = i; EXEC SQL EXECUTE up1 USING :temp1_int ; } print time; /* inline loop */ for (i=1 ; i<10000 ; i++) { temp2_int = i; EXEC SQL INSERT INTO temp2 VALUES (:temp2_int) ; } print time; --- The prepared sql loop takes 13 seconds to execute. The inline sql loop takes 30 seconds. On examining the intermediate C file generated by esql, it appears that, rather than converting the sql into some form of compiled sql, it just passes the strings virtualy unchanged to the database engine. This requires the engine to completely re-interpret the string 10000 times. On the other hand the prepared sql would appear to only have to substitute the value of the host variable. Am I missing something here? Please tell me that there is a compiler switch I should have thrown! Is V7 any better? Peter Jennings.