on 2017 Sep 14 5:43 PM
Hi all,
I've got this (rather large) table (4.2 M rows) that has a foreign key column ("fk_artpres_schemes_id") that contains all "null" values at the moment.
Now from my client application I call a stored procedure that should gather some data for a specific foreign key value ("pi_id" in the example below). It should return nothing specific as there are no values present in the db at all for all non null values. In my opinion this query should finish really fast as no rows match the foreign key value ...
Now the strange part: my application calls this stored proc 30 times (say with id's 1 to 30) and now for most values the sp returns subsecond (as expected), but for some values it takes 20 seconds (!) to complete this query. Most of the time 2 or 3 out of these 30 take way too much time ... (roughly between 5 to 10 run fast and then a (random) value takes for ever)
When I rerun the same 30 queries again it's a another 2 or 3 id's that take a lot of time. While nothing has changed to the db.
I've added an index to this foreign key column, it now reduces the slow queries to roughly 10 seconds, but it's still way too slow (IMHO) for 2 to 3 out of 30 queries.
below is the offending query (that's in a stored procedure). I've currently changed it to select only "1" so it can only be the filtering of the rows that should take time:
If anybody has a clue how to increase the performance on this, be my guest, I'm clueless
TIA
message 'pre Q 1'; select 1, //count(distinct vkreg.fk_par_id), 1, //count(vkreg.id), 1, //sum(if vkreg.aantal > 0 then 1 else 0 end if), 1, //sum(if isnull(vkreg.pres_besteld,0) = 0 and vkreg.aantal > 0 then 1 else 0 end if), 1, //count(distinct if vkreg.aantal > 0 then fk_artnum else null end if), 1 //sum(aantal) into lCnt_modules, lCnt_details, lCnt_details_quantified, lCnt_details_orderable, lCnt_products, lSum_quantity from vkreg where fk_artpres_schemes_id = pi_id; // message 'post Q 1';
the database server is version 11.01.2436
It does look odd, as you say. Just to confirm: is it correct that the values of fk_artpres_schemes_id that give slow and fast results vary from run to run, ie a value that is slow one time can be fast another?
Are you able to capture a graphical plan for a slow and a fast query and see if they are different? Posting plans here can be tricky - see this question for a work around: http://sqlanywhere-forum.sap.com/questions/28174 (basically you have to trick the site into thinking its an image file)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yep, the very same id (value for "fk_artpres_schemes_id") can run fast and slow. When I issue consequetive queries rougly one out of 10 runs slow, and then 10 run fast again.
also: I can't reproduce this in ISQL (issueing 30 queries, gives 30 fast responses (all zero or milliseconds, so really fast)
I'm calling these queries through odbc and I think this has something to do with it (too) ...
b.t.w. the plan looks identical for all queries (and as far as I can tell it uses the index, this is from the plan viewer :
"Index Only Retrieval Scan Scan vkreg using index artpres_schemes_fk_artpres_schemes"
The "slow plan" may only used occasionally, and it may be necessary to capture the plan on-the-fly, multiple times, in order to see the "slow plan". Then you can compare it to the "fast plan" to see what bad choice(s) exist in the slow plan, and perhaps come up with some way to force SQL Anywhere to use the fast plan (e.g., a FORCE INDEX clause).
The Application Profiler / Database Profiler may be used for this process, or the technique starting at Step 8 in this blog post.
Are you able to exclude your application from the equation, while still using ODBC - eg using MSQuery or any other ODBC connection client.
If it's always consistently good with dbisql, you seem to be largely ruling out the database engine itself.
Is anything else going on at the same time in the database? What isolation level does your application use?
> If it's always consistently good with dbisql, you seem to be largely ruling out the database engine itself.
I disagree. Which plan is chosen sometimes depends on the "runtime context" which may be vastly different between the application and dbisql: current contents of the cache, etcetera... it is somewhat analogous to the observer effect in physics.
But Franky is saying that the plan is always the same (and always fast from dbisql). If the plan varied between the slow and the fast runs then I'd agree that something was affecting the engine's choice of plan.
If it's only ever slow from the application, that seems to me like the place to start - I'm thinking in terms of some affecting the connection, its settings, locking etc. Also other activity causing ckeckpoints, I don't know how well Franky can isolate the issue from anything else going on?
I've diven into the application profiler (didn't know it contained that much detail ...):
the fast queries all have this plan:
( Plan [ (B) ] ( WorkTable ( HashGroupBy ( IndexScan vkreg artpres_schemes_fk_artpres_schemes[ 1 = CAST(CAST("dba"."DT_WeeksInWeekRange"(CAST(vkreg.week_vanaf AS int),CAST(vkreg.week_tm AS int),pi_SWeekno,pi_EWeekno) AS unsigned smallint) AS smallint) : 5% Guess ] ) ) ) )
and the slow ones all have this plan:
( Plan [ (B) ] ( WorkTable ( HashGroupBy ( TableScan vkreg[ pi_id IS NOT NULL : 94% Guess ][ vkreg.fk_artpres_schemes_id = pi_id : 0.073472% Column ][ 1 = CAST(CAST("dba"."DT_WeeksInWeekRange"(CAST(vkreg.week_vanaf AS int),CAST(vkreg.week_tm AS int),pi_SWeekno,pi_EWeekno) AS unsigned smallint) AS smallint) : 5% Guess ] ) ) ) )
btw: I run this on a test db, only one connection, no checkpoints interfering, actually nothing I can think off (but I'm no expert :-))
should I simply force the index and forget about it, or are the things to investigate?
That's really useful it shows the problem clearly - the table scan. As Breck said something is occasionally making the database choose a bad plan. If this was a supported version then it be a matter of a support case to try to work out why, but since it isnt a work around getting the server to use the index will be the answer. See if FORCE INDEX does the trick.
One possible reason for a bad plan being used occasionally is there's a limit on the number of plans that are considered before one is picked, and at different times, those candidate plans may be considered in a different order, and the "good plan" may be further back in line.
In any event, it is far more likely that FORCE INDEX will help now, than waiting for an EBF 🙂
User | Count |
---|---|
68 | |
10 | |
10 | |
10 | |
10 | |
8 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.