473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Maximum UNION statements in a query

Wondering if there is a physical or realistic limitation to the number
of UNION statements I can create in a query? I have a client with
approx 250 tables - the data needs to be kept in seperate tables, but I
need to be filtering them to create single results sets. Each table
holds between 35,000 - 150,000 rows. Should I shoot myself now?

lq

Dec 17 '05 #1
15 16774
Hello, lq

There is a limit of "256 tables per SELECT statement". You can use more
than 256 tables in a query with UNION statements, but such a query
cannot be used as a view or as a subquery for a SELECT statement. If
you use such a query directly, you may hit a stack space limit of the
query optimizer at around 1300-1500 SELECT-s.

If you implement partitioned views, make sure that you stay under the
256-tables limit. It would be best if the partitioning column has a
fixed set of possible values (for example, use the first letter of the
country name, not the country name itself).

You may also want to take a look at "partitione d tables" in SQL Server
2005. They are somehow easier to implement and more flexible than
partitioned views. Also, they are subject of a limit of "1000
partitions per partitioned table" (instead of the 256-table limit for
partitioned views).

For more informations, see:
http://groups.google.com/group/comp....e778a5f3affd5b
http://groups.google.com/group/micro...5402088c4da967
http://msdn2.microsoft.com/en-us/library/ms190199.aspx
http://msdn2.microsoft.com/en-us/library/ms143432.aspx

Razvan

Dec 17 '05 #2
Hi

Shoot yourself.

SQL Server 7.0, 2000 and 2005 limits are as follows:
Tables per SELECT statement: 256

So, not only will your queries perform badly, but you walk into a 256 table
limit. No matter if you have views etc, but 256 base tables is all you get.

With correct design, you could have all the data in one table and access the
data through views to enforce security.

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mi**@epprecht.n et

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"laurenq uantrell" <la************ *@hotmail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Wondering if there is a physical or realistic limitation to the number
of UNION statements I can create in a query? I have a client with
approx 250 tables - the data needs to be kept in seperate tables, but I
need to be filtering them to create single results sets. Each table
holds between 35,000 - 150,000 rows. Should I shoot myself now?

lq

Dec 17 '05 #3
Mike,
Thanks for that. Unfortunately, the client architecture rules require
the data to reside in approx 250 different tables.

So, what I'm considering, before I shoot myself, is playing with
something like this (though I suspect the performance will totally blow
with each table holding average of 50K records.):

(foo code)

DECLARE @counter smallint, @tablename varchar(20), @sql nvarchar(4000)
SELECT @tablename = 'tblBaseName'

SELECT @sql = 'SELECT * FROM tblBaseName1'

SET @counter = 2
WHILE @counter < 251 /*max table count*/
BEGIN
SET NOCOUNT ON
@tablename = @tablename + CAST(@counter as varchar(3))
SELECT @sql =

@sql + ' UNION ALL SELECT * FROM ' + @tablename

SET @counter = @counter + 1
SET NOCOUNT OFF
END
GO

EXEC sp_executesql @sql

Dec 17 '05 #4
Never worked with partitioned views before (and not too old to learn
new tricks) but I understand from Books Online that a partioned view
requires a partioned table???
This client is using SQL Server 2000

Dec 17 '05 #5
build a 'temp' table, and union them in one or 10 at a time, then do
your work against the temp table.

will take a little work to make the whole mess re-entrant, but can be
done by creating the temp table name as unique and passing its name
around as a variable.

Dec 17 '05 #6
> I understand from Books Online that a partioned view
requires a partioned table???
This client is using SQL Server 2000
No, a "partitione d view" and a "partitione d table" are different
things. "Partitione d table"-s work only in SQL Server 2005. If you use
SQL Server 2000, you can use a partitioned view. See:
http://msdn.microsoft.com/library/en...es_06_17zr.asp
Unfortunately, the client architecture rules require
the data to reside in approx 250 different tables.


For what purpose ? Performance ? Security ? You should evaluate if the
original reason (for this architecture) is still accomplished best in
this way or if there are another ways to do it.

Razvan

Dec 17 '05 #7
Security, and I can have no control over that structure for this
particular client.

Dec 18 '05 #8
If security was the purpose, you need to consider what rights will be
granted for this partitioned view, so it won't defeat the purpose of
creating the separate tables. After you do this, you should consider
creating a single table that contains the data that would be returned
by the view and create views instead of each old table (using "WITH
CHECK OPTION"). If you do this, you should be able to enforce the same
security policies, but you would have the data in only one place.

Razvan

Dec 19 '05 #9
Stu
If you use partitioned views with CHECK constraints on the tables, your
performance will actually be pretty good. We store about 4 million
rows of data a day, and we keep a partioned view of about 90 days worth
of data; works fine, as long as you supply the appropriated check
constraint to the view when searching. Another benefit is that the
partioned view can be used to UPDATE and INSERT data.

There are some gotcha's (like passing a parameter to the constraint),
but for the most part, partitioned views sound like the solution.

Stu

Dec 19 '05 #10

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

Similar topics

9
5509
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
3
5374
by: Edward | last post by:
I am having conceptual trouble with the following query: select r.ServiceID,r.ContractID,sum(Total) from ( select csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total from iwms_tbl_CustomerSiteContainers csc, iwms_tbl_ContractLines cl, iwms_tbl_Contracts c,
0
5040
by: Stuart E. Wugalter | last post by:
Hello: I have a form (record source: tblMasterTable) containing a MEMO field named "FASTA." I have used a double-click event to trigger a macro that runs the following query: SELECT SNPID, OrderNum, RefNo, FASTA FROM tblMasterTable WHERE Forms!frmFASTA!FASTA=FASTA;
3
3424
by: Chip R. | last post by:
Hi, This isn't really an access question, but as I'm really new to SQL I don't know where I would post this. Any suggestions on that would also be helpful. Now on to the question... I am trying to extract data from a Timberline database through an OBDC driver to MS Excel. I'm using Microsoft Query and a crude knowledge of SQL to build my query.
1
7779
by: CrystalDBA | last post by:
I usually design applications in SQL Server and Crystal Reports. I now need to create a crystal report on an MS Access database. I have two tables: Services: Date datetime Entry text Amount number (should be 4 records)
5
3297
by: Lyn | last post by:
This one is difficult to explain, so I will cut it down to the basics. I have a major table 'tblA' which has an autonum field 'ID-A' as primary key (of no significance to users). 'tblA' contains many fields including picture and memo fields. The main user-selectable field is 'NameA'. There is also a crossreference table (let's call it 'tblB') which provides a secondary method of accessing records in 'tblA'. The main fields in 'tblB'...
2
1912
by: Jonathan Woods | last post by:
Hi there, I have no idea why the following TSQL does not work.Any Idea? I am using SQL 2000 Server CREATE PROCEDURE RptDailySummary2 @FromTxDate DATETIME, @ToTxDate DATETIME AS BEGIN
5
2512
zachster17
by: zachster17 | last post by:
Hi everyone, First of all, sorry for the massive amount of SQL I am about to type. I have a database that has a "lives" table of insured employees and then another table (that links to the lives table) that has all the dependents of the employees. I have also have an alternate ID table in case an employee has more than 1 id. I have to export all the data into a fixed-text-length file with very combination of the above: meaning all lives,...
5
2470
by: slenish | last post by:
Hello all, I am wondering if its possible to do a Union All Query for 1 table instead of mulitiple tables. I have 7 columns that im trying to Union. I have it working to an extent right now but its way to slow. I'm wondering what I can do to speed it up. (Im sorting threw about 300,000 records)(Time it takes is a min or more) I also noticed that its doubling the record count when i do it this way which could be why its taking so long. ...
0
9639
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
10311
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
10146
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...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
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...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
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
5378
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...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.