473,385 Members | 1,912 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,385 software developers and data experts.

Can you add an Index to a table variable?

Joe
Hi,
I've got 2 table variables inside of an SQL 2000 function:
@tmpBigList(BItemID, BRank)
@tmpSmallList (ItemID, Rank)

The following UPDATE statement can run for a long time if @TmpTable1
has 500 rows and @TmpTable2 has 35,000 rows.

UDPATE @tmpBigList
SET BRank = t.Rank
FROM @tmpBigList
JOIN @tmpSmallList t on t.ItemID = BItemID

Looking at the Query Plan, you see that the INNER JOIN Of @tmpBigList
to @TmpSmallList results in 500 * 35,000 = 17,500,000 rows being
returned from @TmpSmallList. That takes a long time.
An index would help, but it appears that you can't add an index to a
table variable.

Changing to a temp table does not work since it's in a function.
Thanks,
Joe Landes

Jul 20 '05 #1
1 2154
On 1 Dec 2004 21:15:00 -0800, Joe wrote:

(snip)
An index would help, but it appears that you can't add an index to a
table variable.


Hi Joe,

That is correct. The only way to create an index on a table variable is to
create a UNIQUE or PRIMARY KEY constraint when declaring the variable.

If neither of your tables allows duplicates in ItemID, the declare would
be like this:

DECLARE @tmpBigList TABLE (BItemID int NOT NULL
,BRank int NOT NULL
,PRIMARY KEY(BITemID)
)
DECLARE @tmpSmallList TABLE (ItemID int NOT NULL
,Rank int NOT NULL
,PRIMARY KEY(ItemID)
)

Note: check the datatypes and nullability agains your requirements.

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

1
by: gregbacchus | last post by:
Hi, Here's my problem: I want to write a stored procedure that returns all records from a table that have a certain column starting with given text. I however find that using LIKE and a variable...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
13
by: LUIS FAJARDO | last post by:
I have the following sintax: Select * From Inventory Where PartId = Coalesce(v_PartId, PartId) this type of query is used within an store procedure that provide the v_PartId parameter, the...
1
by: Gorilla | last post by:
I bound my package with EXPLAIN(YES), and it's got the following static SQL in it: EXEC SQL SELECT CARDF, RECLENGTH INTO :CARDF,:RECLENGTH FROM SYSIBM.SYSTABLES WHERE NAME = :TBNAME AND...
3
by: Eric Davies | last post by:
We've implemented a 5D box data type and have implemented both RTree and GiST access methods under PostgresSQL 7.4 and PostgresSQL 7.4.1. The 5D box internally looks like: struct Box5D{ float...
5
by: siyaverma | last post by:
Hi, I am new to php, i was doing some small chnages in a project developed by my collegue who left the job and i got the responsibility for that, After doing some changes when i run it on my...
1
by: francsutherland | last post by:
Notice: Undefined index: send in D:\Domains\workingdata.co.uk\wwwroot\contact_text.php on line 7 Hi, I've started getting this error in the contact page form of my website. The web hosting...
10
by: Ian | last post by:
Henry J. wrote: MDC *guarantees* clustering, whereas a table with a clustering index will eventually require maintenance (a.k.a. reorg) to maintain the cluster ratio. That's not to say that...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.