<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.talis.com/tdn" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>Talis Developer Network - Trying to make sense of some sql - Comments</title>
 <link>http://www.talis.com/tdn/node/1676</link>
 <description>Comments for &quot;Trying to make sense of some sql&quot;</description>
 <language>en</language>
<item>
 <title>running SQL</title>
 <link>http://www.talis.com/tdn/node/1676#comment-403</link>
 <description>&lt;p&gt;It may require a scripted approach to get what you want.&lt;/p&gt;
&lt;p&gt;I have published an article &lt;a href=&quot;/tdn2/system/files&amp;amp;file=art_sm202db01.pdf&quot;&gt;How to analyse SQL statement performance&lt;/a&gt; that gives an introduction to analysing Sybase performance.  It is obviously an area where care needs to be taken.&lt;/p&gt;
&lt;p&gt;I checked briefly and it looks like when you don&#039;t have the LOAN.CREATE_DATE then an index on the loan table (LOAN_INDA) is used.  But when you do use the condition then as the attribute is not indexed it does a table scan.  Since the LOAN table will be one of the biggest tables you want to avoid the table scans if possible especially as it may have to do multiple scans.&lt;/p&gt;
&lt;p&gt;An approach may be the following SQL&lt;/p&gt;
&lt;p&gt;select L.CREATE_LOCATION, count(L.LOAN_ID)&lt;br /&gt;
from LOAN L (1), ITEM I, CLASSIFICATION C&lt;br /&gt;
where C.CLASS_NUMBER BETWEEN &quot;$1&quot; AND &quot;$2&quot;&lt;br /&gt;
and L.STATE in (0, 1)&lt;br /&gt;
and C.CLASS_ID = I.CLASS_ID&lt;br /&gt;
and I.ITEM_ID = L.ITEM_ID&lt;br /&gt;
group by L.CREATE_LOCATION&lt;/p&gt;
&lt;p&gt;This will list the count for each site which I realise may not be what you are looking for.  The SQL will use the LOAN_INDA index but now requires a work table to do the group by section.  This may mean that is is not quicker (it could take longer) or still not quick enough.  In my tests here it made little difference to the time but then I wasn&#039;t seeing a real difference in the first place between the two origianl pieces of SQL.&lt;/p&gt;
&lt;p&gt;You could give it a go but be very careful if running on the live system.  This is a standard warning whenever you run a piece of SQL.&lt;/p&gt;
&lt;p&gt;The only other option is to run for smaller sections of class numbers and then add together the values.  Again not ideal and may still take too long but a possibility.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Brian Crampton&lt;br /&gt;
Developer, Talis&lt;/p&gt;
</description>
 <pubDate>Fri, 10 Nov 2006 11:41:42 +0000</pubDate>
 <dc:creator>Brian Crampton</dc:creator>
 <guid isPermaLink="false">comment 403 at http://www.talis.com/tdn</guid>
</item>
<item>
 <title>running sql</title>
 <link>http://www.talis.com/tdn/node/1676#comment-400</link>
 <description>&lt;p&gt;Thank you very much for that comment, Brian - I appreciate your comment that there is nothing inherently wrong with the sql because it did seem such an odd thing to happen.  &lt;/p&gt;
&lt;p&gt;The response time is not acceptable because this is to be used as a checking tool by staff for stock, and ideally they would want to get answers to all the aspects of current provision and previous use within a particular Dewey  area within a minute - so for just one part of it to take more than two is no use.  If anyone has got any ideas as to how I could otherwise get this information, I will be very grateful.&lt;/p&gt;
&lt;p&gt;Why do you say that care needs to be taken in the use of indexes rather than full table scan? - and I have not heard of the LOAN_INDA index. I will also be interested in learning more about the query plans - sound a useful tool.&lt;/p&gt;
</description>
 <pubDate>Fri,  3 Nov 2006 15:43:23 +0000</pubDate>
 <dc:creator>vicky</dc:creator>
 <guid isPermaLink="false">comment 400 at http://www.talis.com/tdn</guid>
</item>
<item>
 <title>running SQL</title>
 <link>http://www.talis.com/tdn/node/1676#comment-399</link>
 <description>&lt;p&gt;There is nothing inherently wrong with the SQL.  It is likely to be the way Sybase has decided to run the two queries on your database.&lt;/p&gt;
&lt;p&gt;Whenever you run a query the Sybase database decides on a query plan on how to retrieve the data.  This plan includes what indexes to use on each table.  The plan can potentially vary from customer to customer because the size of tables, the distribution of data etc is different.&lt;/p&gt;
&lt;p&gt;As the database changes it is possible that one day an index is used and then the next day Sybase may decide to table scan rather than use an index. A table scan is more intensive.&lt;/p&gt;
&lt;p&gt;The database is configured e.g. indexes etc so that Alto and other applications are not usually affected by this.&lt;/p&gt;
&lt;p&gt;When I run this SQL here the time is about the same for both queries and both queries use LOAN_INDA index.  It is possible that in your case that this index is used for the first query but for the second query Sybase has decided that it might as well do a table scan of the LOAN table which does more work.&lt;/p&gt;
&lt;p&gt;Without investigation I&#039;m not sure whether this is the exact cause but it could be.&lt;/p&gt;
&lt;p&gt;As you will have noted I was able to check what the query plan was doing for a peice of SQL.  I will put together some details about how this can be checked.&lt;/p&gt;
&lt;p&gt;It is possible to force the use of indexes but this needs to be done carefully.  You don&#039;t mention why the response time is not acceptable and what the context in which the SQL is being used.  There may be other solutions.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Brian Crampton&lt;br /&gt;
Developer, Talis&lt;/p&gt;
</description>
 <pubDate>Wed,  1 Nov 2006 14:59:02 +0000</pubDate>
 <dc:creator>Brian Crampton</dc:creator>
 <guid isPermaLink="false">comment 399 at http://www.talis.com/tdn</guid>
</item>
<item>
 <title>Trying to make sense of some sql</title>
 <link>http://www.talis.com/tdn/node/1676</link>
 <description>&lt;p&gt;I am trying to get some information out, but I fear my sql ability is failing.  I have test run &lt;/p&gt;
&lt;p&gt;select count(L.LOAN_ID) from LOAN L, ITEM I, CLASSIFICATION C&lt;br /&gt;
where  C.CLASS_NUMBER BETWEEN &quot;$1&quot; AND &quot;$2&quot;&lt;br /&gt;
and L.STATE in (0, 1)&lt;br /&gt;
and C.CLASS_ID = I.CLASS_ID&lt;br /&gt;
and I.ITEM_ID = L.ITEM_ID&lt;/p&gt;
&lt;p&gt;and this gives a result in 13 seconds.  However, if I add in a line to include the location as a criterion:&lt;/p&gt;
&lt;p&gt;select count(L.LOAN_ID) from LOAN L, ITEM I, CLASSIFICATION C&lt;br /&gt;
where  C.CLASS_NUMBER BETWEEN &quot;$1&quot; AND &quot;$2&quot;&lt;br /&gt;
and L.STATE in (0, 1)&lt;br /&gt;
and L.CREATE_LOCATION = &quot;ML&quot;&lt;br /&gt;
and C.CLASS_ID = I.CLASS_ID&lt;br /&gt;
and I.ITEM_ID = L.ITEM_ID&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.talis.com/tdn/node/1676&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://www.talis.com/tdn/node/1676#comments</comments>
 <category domain="http://www.talis.com/tdn/taxonomy/term/60">Talis Library System</category>
 <pubDate>Wed,  1 Nov 2006 12:25:53 +0000</pubDate>
 <dc:creator>vicky</dc:creator>
 <guid isPermaLink="false">1676 at http://www.talis.com/tdn</guid>
</item>
</channel>
</rss>
