Question

SELECT INTO create table sql performance issues

Asked by: gagaliya

Hi, i have a sql:  select * into new_table from existing_table where 1 = 0

Basically, it just creates a new table (new_table) wth the same columns as the existing_table. The where 1=0 makes sure no data is selected.  

Now this sql is taking more than 1 hour to execute, when it should be done in a seconds. Why is this taking such a long time?  Correct me if i am wrong, but i dont think it's waiting on locks on the existing_table  as 1) it's a select  2) 1=0 means no data are selected anyway

Dont understand what could possibly causing this sql to take so long to execute.

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-18 at 11:07:38ID24242829
Topic

Sybase Database

Participating Experts
3
Points
500
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Locking tables
    I need a method of doing the following for a multi-user system, accessing oracle through ADO/ODBC: Select the row from a table with the highest value of one of the fields (an integer field); find this highest value and add one to it; insert a new row using this new highest v...
  2. Lock table command in SQL Express 2005
    Hi, We have a stored-procedure on SQL Express 2005 which uses BCP to back-up the data from a table into a CSV file, and then deletes the data from the table. This is done in 2 lines: EXEC xp_cmdshell 'BCP command to back up' DELETE FROM TABLE_NAME The issue is that if ther...
  3. No Hours column? - SQL Express DB
    I have a table named AttendanceGrid in my SQL Express DB. Each student in the table also has a column called "TotalHours" and "Month" Every month of the year there is a new record for every student. January through December. And each of those records s...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: gagaliyaPosted on 2009-03-18 at 11:09:01ID: 23921692

want to add new_table does not exist, select into sql creates the table.

 

by: k_murli_krishnaPosted on 2009-03-18 at 11:20:27ID: 23921849

SELECT acquires only READ/SHARED lock but if existing table has even a single record/block/entire table locked due to already executed UPDATE/DELETE/INSERT and neither committed nor rolled back, your new table creating select has to wait till lock is released by commit/rollback or explicit killing of lock/session.

 

by: k_murli_krishnaPosted on 2009-03-18 at 11:23:01ID: 23921881

It might so have happened that WRITE/EXCLUSIVE lock was acquired by a open UPDATE/DELETE/INSERT which was committed/rolled back after an hour. If no lock exists, definition (I think only structure - no keys/indexes) creation in your style should be over in a jiffy.

 

by: grant300Posted on 2009-03-18 at 14:16:34ID: 23923856

This should return in less time than you can see it happen.  When you put an explicit SARG on a query that Sybase can evaluate at optimization time that will unambiguously prevent any rows from returning, it won't even try to access the data pages; just the catalog tables.

Try a test.  Remove the "into newtable" and just run the "select * from oldtable where 1=2" and see what happens.

A problem like this is very rare and probably indicates you have some not-to-nice problem with your database or application.  Your database might be full, the log might be full, the catalog tables might have a corruption.  None of the things likely to cause this problem are good.  You are just seeing a symptom of something else.

BTW, what version of Sybase are you running?  Post a "SELECT @@version" if you would.  THX

Regards,
Bill

 

by: angelIIIPosted on 2009-03-18 at 14:23:20ID: 23923924

is existing_table  really a table, or a (complex) view?

 

by: grant300Posted on 2009-03-18 at 14:31:50ID: 23924020

Ah, that's a darn fine question and would certainly explain things....

Bill

 

by: gagaliyaPosted on 2009-03-25 at 09:09:58ID: 23981242

"This should return in less time than you can see it happen.  When you put an explicit SARG on a query that Sybase can evaluate at optimization time that will unambiguously prevent any rows from returning, it won't even try to access the data pages; just the catalog tables."

Yes that's exactly why i am confused. I think the previous posters were incorrect, in that it does NOT wait on the insert/update locks due to where 1=2.   Unfortunately this sql is part of our batch process that runs in the middle of the night, which is what makes this very frustratring.  If i run it manually in dbartisan during another time, there is no issue at all. And this issue doesnt happen every night, but often enough to cause a problem.

The existing_table is a normal table, not a view.  I guess what i am looking for is how to go about debugging this, or some possible causes.  Dont have access to db now, but version is ase 12.5

 

by: grant300Posted on 2009-03-25 at 12:29:27ID: 23983614

You will probably have to sit up with this thing at night to see what is really going on.  Perhaps there is something else in the nightly batch stream that is hammering the database and/or locking up resources.  Are there decent logs of the batch operations including the one you are running?  You might want to troll through those and see if there is anything going on that nobody has discovered yet.  Probably most productive to look at the nights when your job hung up.

Another approach would be to put a bunch of diagnostic queries in your job to see what is going on.  Tempdb filling up, log full, CPU bound, I/O bound, huge rollback in progress, etc.  You can also setup sp_sysmon to run during your nightly batch cycle which may uncover other issues.

One worst case is that someone wrote a piece of SQL that actually acquires and holds a lock on one or more system tables in tempdb.  This would be very bad though it is not very likely.

You could also have a TEMPDB that is filling up.  That might prevent the table creation from finishing as there is no place to write the log of the system table changes.

What is the precise version of ASE 12.5?  If you are running 12.5.0, you are not on a stable release and the first thing to do is patch the installation up to something that runs reliably.  Try doing a....
SELECT @@version
and posting the results.  If you are running a late enough version, you can create an additional tempdb and bind your login to that tempdb and see if that helps.

Regards,
Bill

 

by: gagaliyaPosted on 2009-03-31 at 08:52:37ID: 24030257

Hi bill, thanks for the info it's very helpful. I am in the process of adding the monitor to our process.  I will have it to spawn a separate thread and run all the diagnostic sqls then dump the result to me in a bing.

Unfortunately our database is controlled by a separate dba team, and those guys are non responsive whatsoever.  So without admin access, i am limited to execute sqls only, no logs.

I did notice in the code, there are a lot of select * into tempdb..#some_tmp_table from table. I guess that's fine since they are temp tables.  Do you know what happens if there are too many temp tables created in tempdb to fill up all the space at certain time.  Once tempdb is full, does it cause table creation to halt until more space are made availiale in tempdb again? If that's true, then it is most likely what happened i think.

our exact sybase version is 12.5.4

thanks again!

 

by: grant300Posted on 2009-03-31 at 10:39:58ID: 24031477

Yes, if tempdb fills up, anything doing a SELECT/INTO #temptable will be blocked.  Anything writing to an existing tempdb table will be blocked as well.  There should be Sybase log messages to indicate the database is filling up.  It may be your DBA(s) are simply never bothering to check it.

It is not the number of tables in tempdb, it is the total amount of data and log space used.

You can use the sp_spaceused stored procedure or Sybase Central to monitor tempdb and see if it fills up.  You can even write a script that runs say once every 5 minutes for a few hours at night and dumps the results in a file you can check in the morning.

Another approach would be to run sp_sysmon and sample during the time your job(s) run at ngiht.

One thing that usually works in your organizational situation is to make a request for whatever privs you need to troubleshoot this issue.  Certainly, viewing the Sybase ASE server log (not the transaction log) is one of those things but go ahead and make your wishlist.  When/if the DBAs refuse to give them to you, go to your boss and tell him that the problem is a DBA-related issue and that they won't fix it nor give you the privs to investigate it yourself.  One of two things will happen.  Either the DBAs will figure out what the problem is and fix it or they will give you the privs.  If they do nothing, the owness is on your boss to escalate it in IT; something you cannot be expected to do.  Either way, you are not caught in the middle any more.

Regards,
Bill

 

by: grant300Posted on 2009-03-31 at 10:40:19ID: 24031480

Darn,

Meant to spell check that first.

Sorry about that.

Bill

 

by: gagaliyaPosted on 2009-04-01 at 12:50:26ID: 24043199

Thanks bill, 1 last question before i close this out.  When a tempdb gets full, i know you said it prevents the creation of tables etc in tempdb, but does it prevent the creation of normal (non temp) tables (using select into for example) on OTHER databases that resides on the SAME server as tempdb.  

I tried but couldnt find a good answer to this on google. Think it does, but just want to be sure.

 

by: grant300Posted on 2009-04-01 at 14:25:51ID: 24044200

No, not directly.  The real answer is It depends on what is going on in the SELECT portion of the SELECT/INTO.

If the SELECT requires a work table (e.g. is something more complex than SELECT * FROM tblname WHERE 1 = 2) it will be blocked.  Work tables are light-weight tempdb temp tables created and used within a query itself.  They are "light-weight" because ASE does not bother to make entries in the system tables in an effort to speed things up.

A full tempdb is really bad because of the work table issue.  For example, you cannot do a group by in a query in a database when the tempdb is full because there is no place to put the work table.

Regards,
Bill

 

by: gagaliyaPosted on 2009-04-06 at 09:01:42ID: 24078694

Bill, thank you so much for all your help!  all my monitoring coding will be released to production this weekend, then i will know exactly the state of tempdb and lock situation in the middle of the night.

 

by: grant300Posted on 2009-04-06 at 09:14:06ID: 24078842

Let us know what it turns out to be.

Best of luck,
Bill

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...