Tuesday, June 24, 2008

SQL Optimization Tips

SQL Optimization Tips • Use views and stored procedures instead of heavy-duty queries. This can reduce network traffic, because your client will send to server only stored procedure or view name (perhaps with some parameters) instead of large heavy-duty queries text. This can be used to facilitate permission management also, because you can restrict user access to table columns they should not see. • Try to use constraints instead of triggers, whenever possible. Constraints are much more efficient than triggers and can boost performance. So, you should use constraints instead of triggers, whenever possible. • Use table variables instead of temporary tables. Table variables require less locking and logging resources than temporary tables, so table variables should be used whenever possible. The table variables are available in SQL Server 2000 only. • Try to use UNION ALL statement instead of UNION, whenever possible. The UNION ALL statement is much faster than UNION, because UNION ALL statement does not look for duplicate rows, and UNION statement does look for duplicate rows, whether or not they exist. • Try to avoid using the DISTINCT clause, whenever possible. Because using the DISTINCT clause will result in some performance degradation, you should use this clause only when it is necessary. • Try to avoid using SQL Server cursors, whenever possible. SQL Server cursors can result in some performance degradation in comparison with select statements. Try to use correlated sub-query or derived tables, if you need to perform row-by-row operations. • Try to avoid the HAVING clause, whenever possible. The HAVING clause is used to restrict the result set returned by the GROUP BY clause. When you use GROUP BY with the HAVING clause, the GROUP BY clause divides the rows into sets of grouped rows and aggregates their values, and then the HAVING clause eliminates undesired aggregated groups. In many cases, you can write your select statement so, that it will contain only WHERE and GROUP BY clauses without HAVING clause. This can improve the performance of your query. • If you need to return the total table's row count, you can use alternative way instead of SELECT COUNT(*) statement. Because SELECT COUNT(*) statement make a full table scan to return the total table's row count, it can take very many time for the large table. There is another way to determine the total row count in a table. You can use sysindexes system table, in this case. There is ROWS column in the sysindexes table. This column contains the total row count for each table in your database. So, you can use the following select statement instead of SELECT COUNT(*): SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2 So, you can improve the speed of such queries in several times. • Include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement. This can reduce network traffic, because your client will not receive the message indicating the number of rows affected by a T-SQL statement. • Try to restrict the queries result set by using the WHERE clause. This can results in good performance benefits, because SQL Server will return to client only particular rows, not all rows from the table(s). This can reduce network traffic and boost the overall performance of the query. • Use the select statements with TOP keyword or the SET ROWCOUNT statement, if you need to return only the first n rows. This can improve performance of your queries, because the smaller result set will be returned. This can also reduce the traffic between the server and the clients. • Try to restrict the queries result set by returning only the particular columns from the table, not all table's columns. This can results in good performance benefits, because SQL Server will return to client only particular columns, not all table's columns. This can reduce network traffic and boost the overall performance of the query. 1.Indexes 2.avoid more number of triggers on the table 3.unnecessary complicated joins 4.correct use of Group by clause with the select list 5 In worst cases Denormalization Index Optimization tips • Every index increases the time in takes to perform INSERTS, UPDATES and DELETES, so the number of indexes should not be very much. Try to use maximum 4-5 indexes on one table, not more. If you have read-only table, then the number of indexes may be increased. • Keep your indexes as narrow as possible. This reduces the size of the index and reduces the number of reads required to read the index. • Try to create indexes on columns that have integer values rather than character values. • If you create a composite (multi-column) index, the order of the columns in the key are very important. Try to order the columns in the key as to enhance selectivity, with the most selective columns to the leftmost of the key. • If you want to join several tables, try to create surrogate integer keys for this purpose and create indexes on their columns. • Create surrogate integer primary key (identity for example) if your table will not have many insert operations. • Clustered indexes are more preferable than nonclustered, if you need to select by a range of values or you need to sort results set with GROUP BY or ORDER BY. • If your application will be performing the same query over and over on the same table, consider creating a covering index on the table. • You can use the SQL Server Profiler Create Trace Wizard with "Identify Scans of Large Tables" trace to determine which tables in your database may need indexes. This trace will show which tables are being scanned by queries instead of using an index. • You can use sp_MSforeachtable undocumented stored procedure to rebuild all indexes in your database. Try to schedule it to execute during CPU idle time and slow production periods. sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"

Thursday, June 12, 2008

How to Determine CompactFramework Version

If you are developing mobile application for Windows PocketPC and Windows Mobile using Microsoft Compact Framework, you may need to deploy Compact Framework to targeted mobile device in order to run your application. It is crucial to determine what version is currently installed on targeted device and if the installed version fits your application requirement. Different builds may work differently and thus may break your application if installed wrong version. How to Check If Compact Framework Already Installed on Mobile Device There are few ways to check if any compact framework has been installed. However, I will show you the basic ways to determine compact framework installation. 1. On your device, go to My Device -> Windows and run cgacutil.exe. The version will be displayed on screen. 2. Check if \Windows\mscoree.dll file exists. If this file is exists, that means the compact framework has been installed. Now you know Compact Framework already installed on the mobile device and want to know which revision and builds is being installed. How to Determine Compact Framework Version In order to check the version installed, you can access the registry key on targeted mobile. The registry key that you want to look at is HKLM\SOFTWARE\Microsoft\.NetCompactFramework All versions of Compact Framework installed will be listed in this key. Take not that all Compact Framework 1,2 and 3.5 (beta) can co-exists at the same time on the same mobile device. Thus, you may see both versions on the list if both versions already installed. For more information on what the revision number means, please refer to table below. Compact Framework Version List There are 2 tables below that list out all builds and revisions for Compact Framework 1.0 , Compact Framework 2.0 and Compact Framework 3.5. (There won’t be a CF 3.0). Compact Framework 1.0 1.0.2268.00 RTM 1.0.3111.00 SP1 1.0.3226.00 SP2 Recall 1.0.3227.00 SP2 Beta 1.0.3316.00 SP2 Final 1.0.4292.0 SP3
Compact Framework 2.0 2.0.5238.00 RTM 2.0.6129.0 SP1 2.0.7045.0 SP2
Compact Framework 3.5 3.5.7066.0 Beta 1 3.5.7121.0 Beta 2

Requirements for Pocket PC/ Windows Mobile to run .Net Application with SQL Server.

1) To Connect PDA to PC you need ActiveSync software . 2) After Mobile Connected with PC download the SQL Compact 3.1 SDK and coy these 3 cab files from C:\Program Files\Microsoft SQL Server Compact Edition\v3.1\SDK\bin\wce500\armv4i to your device: #sqlce30.wce5.armv4i.CAB #sqlce30.repl.wce5.armv4i.CAB #sqlce30.dev.ENU.wce5.armv4i.CAB 3)Install NetCFSetupv2.msi from PC. It will install .Net Compact framework 2.0 in both PC and Mobile. You can download 1>NetCFSetupv2.msi from Here 2>SSCEDeviceRuntime-ENU.msi from Here

Some Usefule links regarding Pocket PC development

http://pocketpccentral.net/!pocket_pcs.htm#phone http://www.microsoft.com/downloads http://msdn.microsoft.com/en-us/library/fkack1sx.aspx

How can I tell if my Pocket PC has a Pocket PC 2000/2002/2003 or a Windows Mobile 5 or 6 Operating System?

Q: How can I tell if my Pocket PC has a Pocket PC 2000/2002/2003 or a Windows Mobile 5 or 6 Operating System? A: To find out what Pocket PC version you are running follow the steps below: * Select Start, then Settings on your Pocket PC * Choose the System tab, then the About icon * If the copyright in the About screen is 2001 or later, then you have a Pocket PC 2002 or later Pocket PC device. * If the operating system version is 4.0 but less than 5.0 it is a Pocket PC 2003 or 2003 Device * If the operating system version is 5.0 but less than 5.2 it is a Windows Mobile 5 Pocket PC or Pocket PC Phone Device. * If the operating system version is 5.2 or greater is a Windows Mobile 6 device. Windows Mobile 6 comes in three variations: - Classic (touch screen, no phone capabilities) previously 'Pocket PC' - Standard (non-touch screen) previously 'Smartphone' - Professional (touch screen with phone) previously 'Pocket PC Phone Edition'

Wednesday, June 11, 2008

WinCE Problem - .Net compact framework 2.0 installation problem

Solution for following errors (for .net compact framework 2.0): 1) "Installation of netcfv2.wce4.armv4.cab was unsuccessful" in WinCE mobile 2) "This application requires a newer version of the microsoft .net compact framework than the version installed on this device" Normally this error comes when your mobile has older version of compact framework and you are trying to install .net application of newer version. SOLUTION: 1) Install or reinstall NetCFSetupv2.msi in desktop. It will install .net compact framework in both desktop pc and mobile. --have a good day.(RK)