473,804 Members | 3,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to improve performance in this query?

Hey guys,

Here's my situation:

I have a table called lets say 'Tree', as illustred bellow:

Tree
====
TreeId (integer)(ident ity) not null
L1(integer)
L2(integer)
L3(integer)
....
L10(integer)

The combination of the values of L1 thru L10 is called a "Path" , and
L1 thru L10 values are stored in a second table lets say called
'Leaf':

Leaf
====
LeafId (integer)(ident ity) not null
LeatText varchar(2000)
Here's my problem:

I need to lookup for a given keyword in each path of the tree table,
and return each individual column for the paths that match the
criteria. Here's the main idea of how I have this now.

SELECT TreeId,L1,L2,.. .,L10, GetText(L1) + GetText(L2) as L2text + ...
+ GetText(L10) AS PathText
INTO #tmp FROM Tree //GetText is a lookup function for the Leaf table

SELECT L1,GetText(L1), L2,GetText(L2), ...,L10,GetText (L10) FROM #tmp
WHERE
CharIndex(@keyw ord,a.pathtext) > 0

Does anyone would know a better,smart, more efficient way to
accomplish this task? :)

Thks,
Jul 20 '05 #1
1 1246
On 1 Nov 2004 08:23:44 -0800, Silvio wrote:
Does anyone would know a better,smart, more efficient way to
accomplish this task? :)
Hi Silvio.

Yep. Improve your table design. You should normalize down to at least
third normal form. That emans that the repeating group (L1 ... L10) in the
Leaf table has to go in it's own table:

CREATE TABLE Paths
(TreeID int NOT NULL REFERENCES Trees,
PathNo int NOT NULL CHECK (PathNo BETWEEN 1 AND 10),
LeafID int NOT NULL REFERENCES Leaves,
PRIMARY KEY (TreeID, PathNo)
)

If the same leaf can't occur twice in a tree, you could also add a
UNIQUE(TreeID, LeafID) constraint, or make that the primary key. If the
order of the leaves in a tree is unimportant, you can leave out the PathNo
column.

I need to lookup for a given keyword in each path of the tree table,
and return each individual column for the paths that match the
criteria.


Probably something like this:

SELECT P.PathNo, L.LeafText
FROM Paths AS P
INNER JOIN Leaves AS L
ON L.LeafNo = P.LeafNo
WHERE EXISTS (SELECT *
FROM Paths AS P2
INNER JOIN Leaves AS L2
ON L2.LeafNo = P2.LeafNo
WHERE P2.TreeID = P.TreeID
AND CHARINDEX(@keyw ord, L2.LeafText) > 0)
(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4887
by: ALex_1998 | last post by:
Hi Dear All, I have a large query as below: select count (distinct b.bus_acct_id) from M_DATE M1, M_BIZ_ACCT M2, BIZ_ACCT B, C_PRDT_PKG C
3
2377
by: Leader | last post by:
Hi All, I am getting slower performance of select statements in MS SQL. I am finding select statements in MS SQL are even slower than MS ACCESS. Is there any way to improve the performance of select statements in MS SQL by tuning the database of anything else?? Thanks in advance! Hoque
2
4909
by: Jaidev Paruchuri | last post by:
I have a table called work_order which has over 1 million records and a contractor table which has over 3000 records. When i run this query ,it takes long time since its grouping by contractor and doing multiple sub SELECTs. is there any way to improve performance of this query ?? ------------------------------------------------- SELECT ckey,cnam,t1.contractor_id,count(*) as tcnt,
17
14080
by: Felix | last post by:
Dear Sql Server experts: First off, I am no sql server expert :) A few months ago I put a database into a production environment. Recently, It was brought to my attention that a particular query that executed quite quickly in our dev environment was painfully slow in production. I analyzed the the plan on the production server (it looked good), and then tried quite a few tips that I'd gleaned from reading newsgroups. Nothing worked....
7
10819
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL , "Y" REAL NOT NULL , "Z" REAL NOT NULL )
8
3418
by: Michael C | last post by:
Anyone have any hints on improving the performance of C# UI? I'm filling a TreeView and ListView with information returned by a SQLDataReader and information read from the Registry. I'm working on improving the SQL Query execution speed, but have no idea how to improve Registry-reading performance or TreeView/ListView update speed. Any ideas appreciated. Thanks, Michael C.
2
8440
by: lelandhuang | last post by:
I am developing reporting service and using lots of 'LEFT OUTER JOIN', I am worried about the performance and want to use some subquery to improve the performance. Could I do that like below, SELECT * FROM TableA LEFT OUTER JOIN TableB
0
1442
by: Swami | last post by:
I have 2 questions relating to website design in asp .net: 1. In a website that I am building I have everything as a user control. Even the header, which contains the navigation tabs is in a user control which is placed on every page. Originally, the reason why I chose to do it this way (instead of placing the header in a master page) is because my header tabs change dynamically based on who the user is. My question is, am I losing...
5
1708
by: Sangs | last post by:
I have a table called DMPD_Product_Lookup_Dom. It is a lookup table which contains values for certain fields of other tables in the database. This takes long time to run. Is there any way to improve performance of this query ?? SELECT BNAD.Benefit_Admin_Cat_CD AS 'AdminCategory', DOM1.Value_Description AS 'AdminCategoryDesc', BNAD.Benefit_Component_Name_CD AS 'BenefitAdmin', ...
3
3643
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data found.' CREATE OR REPLACE PROCEDURE PROCESS_ANGKASA(REF_NO varchar2)is v_cntr_code varchar2(16); v_receipt_code varchar2(3); start_time number; end_time number;
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.