How To Move Existing Table Into A Different Filegroup Using Tsql Script
Jul 12, 2006
Hi,
How to move existing table(include its constraint and index) into a different filegroup using tsql in Sql Server 2000. We have 1000+ tables in our system and we are planning to move around 500 tables to a new file group, which is available on another SAN drive.
I will be moving production SQL databases (one is 25Gb, the other is 3Gb) to a new server. One of the reasons is the old server has only IDE disks while the new has raid (I don't know the configuration yet). Database 1 has a primary and one other filegroup. I want to spread the disk I/O by moving table A to it's own filegroup on it's own disk. For the purposes of testing I have 1. brought a database onto a test server (using sp_detach_db, copying the files and attaching using sp_attach_db) 2. created a new filegroup 3. created a physical file on disk, allocated space and associated with my new filegroup
my question is how do I now move table A into this file?
How to move tables to the new filegroup which i created. suppose my database has only primary FG and after that i added one FG and due to performance reasons i want to move the existing table to NEW FG. Thanks.
I've create a partition function and a partion scheme for my database. Now I would like to change an existing table to use these partition. The table is replicated. How can I do this?
I want to move an existing table from another. Example B.mdf dbo.xx to C.mdf dbo.xx B.mdf dbo.xx has records - I created a script and install the script for C.mdf dbo.xx C.mdf dbo.xx is empty - All I need now is to populate the records from B.mdf dbo.xx to the empty C.mdf dbo.xx I was telling I could do a cut and paste to copy the dbo.xx Thank you in advance. ~ Peaches ~
I have an PK Clustered index that I would like to move to another filegroup, which will inturn reside on another drive. Can someone point me to sql tsql or somewhere on bol that has the step by step on how to accomplish this? Thanks in advance.
I've found this problem working with a VLDB, six month ago when I install the DBMS (Win2k3 x64+sp2, SQL 2k5 x64 +sp2, 4 dual core processor and 12 GbRAM) I've got 10 disk (actually ten LUN from a Storage Area Network), each 50Gb. I've put TempDB and Transaction Log on two separate 50 Gb disk and put the database on 8 different data file on the 8 disk; I've created each datafile with a size of 50 Gb (autogrowth disable), so my DB has 400Gb space in it's datafile. After a while the datafile began to fill and we decide to add a couple more 50Gb disk where I decide to put to new datafile; now my db is around 430 Gb and I've got this strange situation:
The first 8 datafile now are almost full of data, and obviously they can't growth since they already occupy the whole disk.
The two additional datafile are relatively empty (about 15 Gb each).
As far as I understand now each time that SQL should write to the databse it writes only on the 2 new datafile, and I fear that this can affect performance. I'd like to reorganize the space in order to have 10 datafile, each with 43Gb of data, but I didn't find any instruction/tool able to move data between datafile.
I mange several large databases with multiple files and filegroups. During some maintenance I was trying to remove a filegroup and its files and noticed that I was unable to. It appears that some service broker objects were created on my user defined filegroup.
Looking at the system tables I see several system tables on my user defined filegroup
SELECT o.name +'.' +i.name FROM sysobjects o inner join sysindexes i on i.id = o.id where groupid = 2
How to add some more ranges to existing partition schema and function?
Already My table partitioned on date ranges,
6 partitions , each partition contains 6 moths data, so total data is 3 years.
i.e. 1 partition data- from jan2012 to Jun2012 2 partition data- from july2012 to dec2012 3 partition data- from jan2013 to Jun2013 4 partition data- from july2013 to dec2013 5 partition data- from jan2014 to Jun2014 6 partition data- from july2014 to dec2014 After Jan2015 data will go to Primary file group(Default)
Now customer wants to add two more file groups with these partitions ranges, i.e. jan2015 to jun15 and Jul15 to dec15.
File group and ndf file adding is OK, But
how to alter partition scheme and partition function with these additional ranges to existing partition function and scheme?
I need to modify a table to reside on a new filegroup and also point TEXTIMAGE_ON to that filegroup instead of PRIMARY. Apparently in the past, the only way to achieve this via SQL is to create a new table, copy over data, drop the old table and rename the new table to the original name. I found this solution in the SQL Server 2005 forum.
Is there any other way to alter this table in order to point the TEXTIMAGE_ON to new filegroup using SQL Server 2014? We are on Standard edition. The technique I am using is the drop constraint (with move option) and add constraint (to new filegroup) commands. The data and indexes move, but not the text data (it still is in primary filegroup).
We are running SQL Server 2014 Enterprise Edition (64-Bit) on Windows 2012 R2 Standard (64-Bit).
1. When to create indexes, before or after data is added? Please address Clustered and Non-Clustered Indexes.
2. To move indexes to it's own filegroup, is it best to create the NON-Clustered Indexes on the separate filegroup with code similar to the example below?
CREATE NONCLUSTERED INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee (OrganizationLevel, OrganizationNode) WITH (DROP_EXISTING = ON) ON TransactionsFG1; GO
I have read the following links that states that if you create the Clustered Index on a separate filegroup, it would also move the base table to that particular filegroup. (So I take it that you ONLY can move NON-CLustered Indexes to a separate filegroup.)
Placing Indexes on Filegroups:
[URL]
By default, indexes are stored in the same filegroup as the base table on which the index is created. A nonpartitioned clustered index and the base table always reside in the same filegroup. However, you can do the following:
• Create nonclustered indexes on a filegroup other than the filegroup of the base table.
Move an Existing Index to a Different Filegroup:
[URL]
Limitations and Restrictions
• If a table has a clustered index, moving the clustered index to a new filegroup moves the table to that filegroup.
• You cannot move indexes created using a UNIQUE or PRIMARY KEY constraint using Management Studio. To move these indexes use the CREATE INDEX statement with the (DROP_EXISTING=ON) option in Transact-SQL.
Typically small companies or small projects which use SQL Server / IIS / File Shares / .NET solutions deploy these apps on a single server many times using just one IP address and the NETBIOS name of the server for both IIS / SQL. Many times there is no use of DNS and even when there is everything is mapped to the server/netbios name.
As environments grow and there is a requirement to create an n-tier server setup possible due to new security requirements, extention of the intranet to the internet, or to scale out an application you may need to split IIS / SQL into different servers. The question then becomes how do you do this without affecting existing services and you want to have minimal down time without having to change all connection information within deployed apps until you have time to review the changes carefully.
First let me begin with how to move IIS away from SQL and then I'll discuss the reverse.
The best way I've found to control the move of IIS sites is by assigning each site it's own ip address / dns host record and adding the ip address to the server network card and IIS site. Now each time you need to move 1 or more sites you can simply replicate the site to another server and change the dns ip pointer to the new pointer. If you have existing sites which were not setup in this fashion you can also move them to a new server, create a site with name of the old netbios machine name, and redirect the default web site to this new site.
Now you successfully isolated SQL Server from IIS/.NET apps you can take your time and correct the apps to use the new dns entry in connection strings instead of using the netbios name of the machine. Once you have corrected the app you can turn off any redirection.
Now let's assume you have the opposite scenario where you started off with a small server and your SQL database server now requires a server with more memory / more processors and you want to keep IIS / FileShares were they are and want to move SQL Databases to a new server. As I mentioned above a good way to do this is by setting up each database with it's own dns entry a very effective redirection mechanism. However you are may be inheriting a system which does not employ this mechanism and again uses the default netbios machine name, furthermore this may get more complicated because there may be multiple instances of SQL Server running on different port numbers. You could use SQL Aliases on the clients to redirect connections to the newly created instances but this would require changing every pc and IIS server and it would be cumbursome to manage. It would have been nice if microsoft provided a way to redirect from the server just like IIS does but they did not. A product which will help you setup a server based redirection of ports 1433/1434 and/or any other ports is called porttunnel by steelbytes.com. Once you've installed the redirecting service on the server all new requests on port the ports selected will be redirected to the new server which I recommend be redirected to their own dns entries and this will buy the applications developer time to change their applications.
I would like to hear any comments or experiences with this that any of you may have had or if anyone has found a better solution. I'm in no way associated with the product / or company mentioned above just found it to be a good solution.
I need to know if it is possible to move ONE table from an existing filegroup to another existing filegroup. The answer I received to use ALTER database only modifies the file/filegroup name or changes the default filegroup. Any assistance will be greatly appreciated.
I am running SQL Server 2005 Enterprise Edition, i want to split my data and indexes on different drives.
In 2000 i had to recreate clustered indexes and non clustered indexes on the correct filegroups to accomplish this.
In 2005 i see there is a ALTER TABLE MOVE TO Filegroup option, thats cool.
Does this effectively do the same as rebuilding the clustered index on the new filegroup? Will this leave the other indexes of the table on the primay filegroup or move them as well ?
If i wanted to also move the non clustered indexes is there a better way to move them that drop and re-create on the new filegroup in 2005, i see the ALTER INDEX statement does not support a move to filegroup option.
In a nutshell what is the best/easiest way to move exisitng table data and indexes to new file groups in Sql Server 2005 Enterprise Edition?
I'm being asked to create multiple filegroups for a new database based on the table type, transaction, lookup, misc... From what i'm reading this doesn't make sense. I'm reading either large tables get file groups, nonclustered indexes when they are about the same size of the data, or a few other reasons...
First of all, we are talking about the same disk (please don't ask me about how it is configured) and I'm not sure yet if restoring separate file groups is even going to be necessary.
So here are my questions (beyond, the test and see what happens) because in the end I'm going to probably have to do what i'm told. So this is for my professional knowledge.
1. Does file groups separated by table type make sense? 2. Should you put tables that are queried often together in the same or different file groups. 3. I'm pretty sure you can't restore single file group for write access, am I correct?
I am using SQL Server 2000, SP3.I created an updatable partitioned view awhile ago and it has beenrunning smoothly for some time. The partition is on a DATETIME columnand it is partitioned by month. Each month a stored procedure isscheduled that creates the new month's table, and alters the view toinclude it. Again... working like a charm for quite some time.This past weekend I moved some of the first tables onto a new filegroup. I did this through Enterprise Manager, by going into designmode for the table, then going into the properties for the table andchanging the file group there as well as in all of the indexes. Nowthe partitioned view is no longer updatable. It gives the errormessage: "UNION ALL view '<view name>' is not updatable because apartitioning column was not found."I have extracted the DDL for all of the partition tables and comparedthem and they all look the same. I checked and then double-checked theCHECK constraints to make sure that they were all valid and they are.If I remove the tables that I moved to the new file group from theview, then it is once again updatable, but when I put them back in itfails again.Any ideas? If you would like samples of the code then I can send italong, but it's rather large, so I have not included it here.Thanks!Thomas R. Hummel
Using SQL against a DB2 table the 'with' key word is used todynamically create a temporary table with an SQL statement that isretained for the duration of that SQL statement.What is the equivalent to the SQL 'with' using TSQL? If there is notone, what is the TSQL solution to creating a temporary table that isassociated with an SQL statement? Examples would be appreciated.Thank you!!
Hi,I am expanding our data warehouse solution with new filegroups onseveral subsystems.I want to know which idea is better!- create clustered indexes on tables to 'move' them to new filegroups- create these tables on the new filegroups.The background of this question is as follows:- we want the whole data on the new filegroups- we want to know if there is any difference in performance between the2 solutionsThanks in advance,Danny
hi all, is there any query to move certain data from a sql data to access table through query. i am having a requirement where i have to fetch the records from a sql table that falls within a specified range to a ms access table. is this possible through a query.
Hi all, please help. I m trying to create an "empty" table from existing table for the audit trigger purpose. For now, i am trying to create an empty audit table for every table in a database named "pubs", and it's seem won't work. Please advise.. Thanks in advance.
SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME NOT LIKE 'audit%' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME = 'sales'
WHILE @TABLE_NAME IS NOT NULL BEGIN
SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME> @TABLE_NAME AND TABLE_NAME = 'sales'
SELECT @TABLE_NAME= MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME> @TABLE_NAME AND TABLE_TYPE= 'BASE TABLE' AND TABLE_NAME!= 'sysdiagrams' AND TABLE_NAME!= 'Audit' AND TABLE_NAME NOT LIKE 'audit%'
i have 2 tables (both containing the same column names/datatypes), say table1 and table2.. table1 is the most recent, but some rows were deleted on accident.. table2 was a backup that has all the data we need, but some of it is old, so what i want to do is overwrrite the rows in table 2 that also exist in table 1 with the table 1 rows, but the rows in table 2 that do not exist in table one, leave those as is.. both tables have a primary key, user_id.
I'd like to create a temporary table with the same schema as an exiting table. How can I do this without hard coding the column definitions into the temporary table definition?
I'd like to do something like:
CREATE TABLE #tempTable LIKE anotherTable
..instead of...
CREATE TABLE #tempTable (id INT PRIMARY KEY, created DATETIME NULL etc...