View previous topic :: View next topic |
Author |
Message |
jim haire Beginner
Joined: 30 Dec 2002 Posts: 140 Topics: 40
|
Posted: Thu Mar 24, 2016 11:57 am Post subject: Select without using a cursor and performance hit |
|
|
I am doing some performance tuning on a COBOL program. There is a SELECT statement that is doing some existence checking.
I know that there can be more than 1 row returned from the SELECT statement. In fact, the developer actually checks for -811. I am going to add a "FETCH FIRST 1 ROWS ONLY" statement so once it finds the first row the query would stop. I am also not returning data from the table...just doing a "SELECT 1". Both of these are for performance reasons.
My question is this:
As the program was originally written, did the query return a maximum of two rows and then realize that the COBOL program was not properly equipped to handle the query (no cursor provided) or would the query determine the complete result set before returning the -811 SQLCODE? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu Mar 24, 2016 1:27 pm Post subject: Re: Select without using a cursor and performance hit |
|
|
jim haire wrote: | My question is this:
As the program was originally written, did the query return a maximum of two rows and then realize that the COBOL program was not properly equipped to handle the query (no cursor provided) or would the query determine the complete result set before returning the -811 SQLCODE? |
Jim Haire,
The result set is NOT materialized before the query is run. FETCH FIRST 1 ROW is a better alternative and if you are merely checking the existence, why not use SYSIBM.SYSDUMMY1 which always have just 1 row. _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
misi01 Advanced
Joined: 02 Dec 2002 Posts: 629 Topics: 176 Location: Stockholm, Sweden
|
Posted: Mon Mar 28, 2016 1:53 am Post subject: |
|
|
Kolusu. I'm going to assume that Jim needs to know whether, say, column_d in mytable has any rows with a value of X. On that basis, using sysdummy1 won't help him (or am I missing something) _________________ Michael |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Mon Mar 28, 2016 5:29 pm Post subject: |
|
|
misi01 wrote: | Kolusu. I'm going to assume that Jim needs to know whether, say, column_d in mytable has any rows with a value of X. On that basis, using sysdummy1 won't help him (or am I missing something) |
Here is the SQL I am talking about
Code: |
SELECT IBMREQD
FROM SYSIBM.SYSDUMMY1
WHERE EXISTS (SELECT *
FROM SYSIBM.SYSTABLES
WHERE CREATOR = 'SYSIBM')
;
|
If you have any records that match the criteria, then you will have 1 record with "Y" or if your table does not contain any rows that do not match the criteria , you will get an sql code of +100.
You can use SELECT 1 instead of SELECT * in the outer query. So it does not matter how many records your outer query brings up, but the final from Sysibm.sysdummy will either be 1 or no records at all. _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
jim haire Beginner
Joined: 30 Dec 2002 Posts: 140 Topics: 40
|
Posted: Mon Apr 04, 2016 12:56 pm Post subject: |
|
|
Does the inner SELECT in this statement only return 1 row before the EXISTS is performed and then stops since a record was found or does the inner SELECT return all rows where the CREATOR = 'SYSIBM'? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Mon Apr 04, 2016 1:22 pm Post subject: |
|
|
jim haire,
Technically EXISTS should stop right after finding 1 row, but it may also depend on your WHERE clause in the outer query. You should run an EXPLAIN on both your original query and the EXIST query and pick the most optimal query. _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
|
|