473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to optimize the sql query the following query is running slow as i have used the

the following query is running slow as i have used the abs function in it

Expand|Select|Wrap|Line Numbers
  1. WITH SubFolders(dataid,name,SubType,lvl)
  2. AS
  3.  
  4.   -- Anchor Member (AM)
  5.   SELECT dataid, Name,subtype, 0
  6.   FROM llirimsuser.DTree (nolock)
  7.   WHERE dataid = 28264085
  8.   UNION ALL 
  9.   -- Recursive Member (RM)
  10.   SELECT D.DataID,D.Name,D.SubType,M.lvl+1
  11.   FROM llirimsuser.DTree (nolock)  AS D
  12.     JOIN SubFolders  AS M
  13.       ON abs(D.ParentID)= M.dataid
  14. )
  15. SELECT * FROM SubFolders where SubType=412
any idea how to optimize it I have to use the abs function it as i am getting negative value
plz help
Mar 28 '14 #1
6 1469
Rabbit
12,516 Expert Mod 8TB
Why not fix the data? Update the negative values to positive.

But I suspect that's not the real cause of the slowness. It's more likely the recursive joins.
Mar 28 '14 #2
Luuk
1,047 Expert 1GB
I hope you have the correct indexes....

Try replacing lines #11..#14 with:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (
  2.  SELECT D.DataID,D.Name,D.SubType,M.lvl+1
  3.   FROM llirimsuser.DTree (nolock)  AS D
  4.     JOIN SubFolders  AS M
  5.       ON D.ParentID= M.dataid
  6.  UNION
  7.  SELECT D.DataID,D.Name,D.SubType,M.lvl+1
  8.   FROM llirimsuser.DTree (nolock)  AS D
  9.     JOIN SubFolders  AS M
  10.       ON -D.ParentID= M.dataid
  11. )
  12.  
Mar 28 '14 #3
@lucck I tried to follow yours approach and replaced the code
with the following
Expand|Select|Wrap|Line Numbers
  1. WITH SubFolders(dataID,ParentID,Name,Comment,SubType,Unit,lvl)
  2. AS
  3.   -- Anchor Member (AM)
  4.   SELECT DataID ,ParentID,Name,DComment,
  5.     Subtype,(select name from llirimsuser.DTree(nolock) where dataid=ownerid) as 'Unit', 
  6.  
  7.   FROM llirimsuser.DTree (nolock)
  8.   WHERE DataID =  28264085
  9.   UNION ALL 
  10.   -- Recursive Member (RM)
  11.       SELECT * FROM (
  12.  SELECT D.DataID,D.ParentID,D.Name,D.SubType,M.lvl+1
  13.   FROM llirimsuser.DTree (nolock)  AS D
  14.     JOIN SubFolders  AS M
  15.       ON D.ParentID= M.dataid
  16.  UNION 
  17.  SELECT D.DataID,D.ParentID,D.Name,D.SubType,N.lvl+1
  18.   FROM llirimsuser.DTree (nolock)  AS D
  19.     JOIN SubFolders  AS N
  20.       ON -D.ParentID= N.dataid
  21. )
  22.  
on line 20 error is coming as 'Recursive member of common table expression has multiple recursive referense' can you help me?
Index is correct the problem of coming of executing query slowly arnd 2 min is coming only when we are using abs() function otherwise its running fine completing 0.02 sec
Mar 31 '14 #4
Luuk
1,047 Expert 1GB
Wait....

This is about MySQL?

MySQL does not support 'WITH..' (line#1)

It is hard to find the problem without the same tables, and some sample data, but basically try to rewrite your original query in a way that it will return the data you want

you can alsow trye to replace
Expand|Select|Wrap|Line Numbers
  1. JOIN SubFolders  AS M
  2.       ON abs(D.ParentID)= M.dataid
with:
Expand|Select|Wrap|Line Numbers
  1. JOIN SubFolders  AS M
  2.       ON D.ParentID= M.dataid OR -D.ParentID= M.dataid
Apr 1 '14 #5
@Luuk--thnks 4 reply this query is for sql server

I have used
Expand|Select|Wrap|Line Numbers
  1. JOIN SubFolders  AS M
  2.           ON D.ParentID= M.dataid OR -D.ParentID= M.dataid
  3.  
as suggested but it is taking same time to execute as with using abs function previously.;-)
Apr 1 '14 #6
Luuk
1,047 Expert 1GB
ok,

@Rabbit: Could you move this topic to mssql server? ;-)

@Isauravbi: I'm not good at mssql server, but you should look at the execution plan of your query, and decide wif you have the proper indexes* on the tables involved.

*) 1 Index , 2 Indexes ??...... English is hard... ;)
Apr 1 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: laurenq uantrell | last post by:
I am trying to determine which of three stored procedure designs are fastest in the Query Analyzer: One query is a straight SELECT query with all desired rows and a dozen (tblName.RowName =...
6
by: johntarr | last post by:
I hope I am not asking about something that has been done before, but I have searched and cannot find an answer. What I am trying to do is to run a query, and then perform some logic on the...
2
by: Eckhart | last post by:
Dear All, Plz help me in optimising the following query, Reduce repeatable reads from the table via select ,ythe table sare not having referntial integrity constarints ,relations CREATE proc...
2
by: windandwaves | last post by:
Hi Gurus I have the following query: DELETE ITEM.* FROM ITEM LEFT JOIN EXTRA_DETAILS ON ITEM.item_code = EXTRA_DETAILS.object_code WHERE EXTRA_DETAILS.field_value IS NULL;
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
2
by: kshirsagar007 | last post by:
friends, I would like to optimize the following query....as its taking 2 minutes to get the records. select a.CODE "Code", ud.Name "Name", a.SchemeId "SchemeID" from (MASTER sh,...
11
by: bravo | last post by:
hi i need to optimize the join query, which joins three tables say table1 ,table2 , table3 each having huge volume of records... the query is as select table1.id,table2.time,table3.Status from...
1
by: rajeev14th | last post by:
How can i modify the following query in DB2 (CAST(SUBS_REFILL_COUPONNO_CHANNELID AS INTEGER) (FORMAT '9(16)') (CHAR(16)) ) where SUBS_REFILL_COUPONNO_CHANNELID is the column name of my table...
7
by: David Goldstraw | last post by:
I have two databases and I want to automate a process to find duplicates. I originally linked the table from one into the other but I have now gone down the route of importing the table from one db...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.