Showing posts with label Database Tips. Show all posts
Showing posts with label Database Tips. Show all posts

Monday, 6 October 2008

ANSI JOIN vs. OUTER JOIN

Q

Can I improve performance by using the ANSI-style joins instead of the old-style joins?



A

You will not get any performance gain by switching to the ANSI-style JOIN syntax. However, when you are dealing with outer joins, the old-style JOIN (=* or *=) is not the exact equivalent of the ANSI-style JOIN.

We strongly suggest that you move to the ANSI-style JOIN syntax. Although old habits are hard to break (we know, we've been there), we've found that after you've become comfortable with the ANSI-style JOIN, you'll quickly prefer using it to code queries. Using the ANSI-JOIN syntax gives you an important advantage: Because the join logic is cleanly separated from the filtering criteria, you can understand the query logic more quickly.

One thing to be aware of is that you should not expect to be able to mechanically convert an old-style OUTER JOIN to an ANSI-style JOIN and receive the same output. You might not replicate the results, because the SQL Server old-style JOIN executes the filtering conditions before executing the joins, whereas the ANSI-style JOIN reverses this procedure (join logic precedes filtering).

Perhaps the most compelling argument for switching to the ANSI-style JOIN is that Microsoft has explicitly stated that SQL Server will not support the old-style OUTER JOIN syntax indefinitely. Another important consideration is that the ANSI-style JOIN supports query constructions that the old-style JOIN syntax does not support.

Backing Up to a File on a Network Share

Q

I tried to back up my databases to a file on a network share and restore the databases on the secondary server. I tried to use the net use command to create the share on the target server, as in:

NET USE * \\PS5\C /USER:ONEDEV\domain idpassword /PERSISTENT:NO

But I keep getting the message "There are no available drive letters left." However, SQL Server Enterprise Manager shows many available drives. How can I fix this problem?

A

Just back up your databases directly to the Uniform Naming Convention (UNC) path. SQL Server does not view mapped drive letters the same way a user does. You need to grant the SQL Server service account full control permissions on your share \\PS5\C, make a new share, or make the SQL Server service account a member of the administrators group on the PS5 computer.

Monday, 22 September 2008

Avoiding the Autoclose and Autoshrink Options


Q
In the Microsoft SQL Server 2000 Resource Kit, I read about a procedure that checks all databases for options that you should avoid using. I understand why I should avoid using options such as offline, DBO-use only, statistics off, truncate log on checkpoint, and so on. However, the inclusion of two of the options—autoclose and autoshrink—puzzles me.

Autoclose. Closing a file when it is not in use sounds like a prudent thing to do. Yet I see files opening and closing all the time in the error log in SQL Server, even though I know that I never use the database except for viewing in Enterprise Manager. Does viewing a list of databases in Enterprise Manager make the files open? It makes sense to me to use autoclose on databases that I use only occasionally as import staging areas, and which I access for only a fraction of the time my online transaction processing (OLTP) database is in use. SQL Server Books Online says that the autoclose option is automatically set to YES for the SQL Server Personal Edition and to NO for all other editions. Has this option been included only for the Personal Edition?

Autoshrink. The autoshrink option also sounds helpful. However, would using autoshrink on my import staging areas, where I bring in quantities of data for short periods of time, keep these areas small for backups?

A
Autoclose is available in all editions of SQL Server 2000. The amount of memory you save by using this option is relatively small but might be important on memory-constrained systems running the Personal Edition. However, enabling autoclose for the other editions isn't a good idea, because every time you try to access the database, the query will be slowed by the amount of load time it takes to mount the database. As for the question about your files opening and closing, yes, when you enumerate a database in Enterprise Manager, you access the database, which opens the files.

Leaving autoshrink on for production systems also isn't a good practice (unless you really need to), because autoshrink might start when your system is otherwise busy with production work, and slow down the system. However, enabling this option is a good idea for desktop or remote systems that don't get a lot of DBA attention and whose databases could grow out of control before you detect the problem.