Search

Wednesday, August 7, 2013

How do I fix "Cannot connect to WMI Provider" error?

The following article explains how to fix the following error message:

Cannot connect to WMI provider. You do not have permission or the server is unreachable.
Note that you can only manage SQL Server 2005 servers with SQL Server Configuration Manager.
Invalid Namespace.


This error is caused when the .mof files are damaged during the MS SQL Server 2005 installation. The error will typically occur when trying to run the MS SQL 2005 Configuration Manager. This error can be resolved by running a command line script.

To fix this error, please follow these steps:

  1. Log into your server through Remote Desktop Connection (instructions for connecting to your server through RDC can be found here).
  2. From the Start menu, select Run.
  3. Type cmd and click OK.
  4. Change to the directory c:\Program Files\Microsoft SQL Server\90\Shared\
  5. Type mofcomp "C:\Program Files\Microsoft SQL Server\90\Shared\sqlmgmproviderxpsp2up.mof" and press enter.
Refer MS KB article: http://support.microsoft.com/kb/956013

You should no longer receive the above error message.

Friday, June 7, 2013

Configure SSIS to use Named Instances

SQL Server family has various services like Database Services (SQL Server), Analysis Services, Reporting Services which supports Named Instance of the services but there are few others like Integration Services which does support named instance. This means, we can have one and only one service of Integration Services on each server.

By default, the MSDB folder of Integration Services points to MSDB of default instance of SQL Server. But we can change this to point any (one or more than one) named instance of SQL server.

To point MSDB folder of Integration Services to any other instance of SQL Server, we need to change the configuration file (MsDtsSrvr.ini.xml). Following are the steps to do that:

 1.Browse the file MsDtsSrvr.ini.xml. Depending on Version of SQL Server installed, you can get this at C:\Microsoft SQL Server\90\DTS\ or C:\Microsoft SQL Server\100\DTS\

2.Create a backup copy of the file.

 3.Open the configuration file in a text editor like BIDS, NotePad.

 4.To configure a named instance instead of the default instance, change the existing ServerName entry to ServerName\InstanceName under the Folder node of type SQLServerFolder.

 5.To configure a named instance in addition to the default instance, add a new Folder node of type SQLServerFolder under the TopLevelFolders node.

 6.Save and close the configuration file.

 7.Restart the SSIS service.
 

Friday, May 24, 2013

Important Scripts to find out Replication details.

Script to run on Distribution database

This script returns completed setup replication information. Unless an orphan article exists, this will return a complete set of replication information. I also added the distribution agent job name to show how easy it is to pull in other configuration information.

USE Distribution
GO
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
-- Get the publication name based on article
SELECT DISTINCT
srv.srvname publication_server
, a.publisher_db
, p.publication publication_name
, a.article
, a.destination_object
, ss.srvname subscription_server
, s.subscriber_db
, da.name AS distribution_agent_job_name
FROM MSArticles a
JOIN MSpublications p ON a.publication_id = p.publication_id
JOIN MSsubscriptions s ON p.publication_id = s.publication_id
JOIN master..sysservers ss ON s.subscriber_id = ss.srvid
JOIN master..sysservers srv ON srv.srvid = p.publisher_id
JOIN MSdistribution_agents da ON da.publisher_id = p.publisher_id
AND da.subscriber_id = s.subscriber_id
ORDER BY 1,2,3



Script to run on Publisher database

This script returns what publications has been setup. This will go through all the published databases and return information if the database has replication enabled. Sometimes, I just want to see the publication name and subscriber server names (no articles) to see what servers are being used with replication other times I want all of the information, so I added a variable called @Detail and if you set @Detail = 'Y' it will return data with the article list. Any other value will only return the publisherDB, publisherName and SubscriberServerName.
-- Run from Publisher Database
-- Get information for all databases


DECLARE @Detail CHAR(1)
SET @Detail = 'Y'
CREATE TABLE #tmp_replcationInfo (
PublisherDB VARCHAR(128),
PublisherName VARCHAR(128),
TableName VARCHAR(128),
SubscriberServerName VARCHAR(128),
)
EXEC sp_msforeachdb
'use ?;
IF DATABASEPROPERTYEX ( db_name() , ''IsPublished'' ) = 1
insert into #tmp_replcationInfo
select
db_name() PublisherDB
, sp.name as PublisherName
, sa.name as TableName
, UPPER(srv.srvname) as SubscriberServerName
from dbo.syspublications sp
join dbo.sysarticles sa on sp.pubid = sa.pubid
join dbo.syssubscriptions s on sa.artid = s.artid
join master.dbo.sysservers srv on s.srvid = srv.srvid
'
IF @Detail = 'Y'
   SELECT * FROM #tmp_replcationInfo
ELSE
SELECT DISTINCT
PublisherDB
,PublisherName
,SubscriberServerName
FROM #tmp_replcationInfo
DROP TABLE #tmp_replcationInfo



Script to run on Subscriber database

This script returns what article(s) is/are being replicated to the subscriber database. I also use this to find orphaned subscribers. This is rather simple since there is not much information to pull.

 
-- Run from Subscriber Database

SELECT distinct publisher, publisher_db, publication
FROM dbo.MSreplication_subscriptions
ORDER BY 1,2,3




The following scripts will provide the name of the database on which merge replication is setup

select namefromsys.databaseswhereis_merge_published= 1

Drop article from Existing Publication (Transactional Replication)


EXEC sp_dropsubscription
@publication = 'Publication name',
@article = N'Table Name',
@subscriber = 'Subscriber Server Name';
 GO

 EXEC sp_droparticle
 @publication = 'Publication name',
 @article = N'Table Name',
 @force_invalidate_snapshot = 0

Adding new article to Existing Publication (Transactional Replication)


Adding new article to Existing Publication (Transactional Replication)

First of all run the Exec sp_helppublication in publication database and checked the following fields

1. Immediate_sync
2. Allow_anonymous

Both the fields were set to ON as they showed a value 1 which is enabled. If the Immediate_sync is enabled, every time you add a new article it will cause the entire snapshot to be applied and not the one for the particular article alone. Usually, the Immediate_sync publication property is set to true if we allowed anonymous subscriptions while creating the publication through the Create Publication wizard. To prevent the complete snapshot, run the script below.


Step 1:- Disable the two fields in publication database
 
EXEC sp_changepublication
@publication = ‘Publication name’,
@property = N'allow_anonymous',
@value = 'false'
GO

EXEC sp_changepublication
@publication = Publication name’,
@property = N'immediate_sync',
@value = 'false'
GO

Step 2:- Add new article in publication database
    exec sp_addarticle
    @publication = N'Publication name’,
    @article = N'Table Name',
    @source_object = N'Table Name',
        @destination_table = N'Table Name'
         GO

exec sp_addsubscription
          @publication = N'Publication name’,
          @subscriber = N'Subscriber Server Name',
          @destination_db = N'Destination Database Name',
          @subscription_type = N'Subcription Type' [Pull\Push]


Step 3:- Now start the snapshot agent in publisher, it worked perfectly. You can see that only the particular table added was replicated. So from now on to apply the snapshots of the entire articles you need to reinitialize the subscriptions since the Immediate_sync is set to off.

Friday, May 10, 2013

IBM’s entrant in the SQL-on-Hadoop

IBM’s entrant in the SQL-on-Hadoop competition has been flying under the radar, but is available as a technology preview. Called Big SQL, it’s a big deal if IBM wants to be a major player in the Hadoop space.

IBM has a SQL-on-Hadoop product in the works called Big SQL. The company announced the technology preview version in March, and is offering up a cloud-based demo environment for a select group of early users.

As a refresher, the big difference between SQL on Hadoop and the Hadoop connectors that were popular a couple years ago is that SQL-on-Hadoop products query the data where it resides — in HDFS or HBase — rather than pulling it into a relational database environment to analyze it. We have been talking for months about the emergence of a large SQL-on-Hadoop market, but IBM’s name was conspicuously absent from that discussion. The company has Hadoop software called BigInsights and lots of SQL expertise, so it only made sense that IBM would get into the game at some point.

Details on Big SQL are still pretty sparse save for a few high-level blog posts and an instructional video (embedded below), but it looks to take the standard approach, as Cloudera is doing with Impala, of enabling access through traditional tools via JDBC and ODBC drivers.

Ultimately, I think the advent of big data will enable some new types of querying techniques quite a bit different than the SQL queries we’ve come to know and love over the past couple decades. But SQL is still the language du jour and might never go away, so there’s a lot of value to be had if people can put their SQL skills to work on data stored inside Hadoop or other environments, and if companies can work toward a nirvana where all the data is stored in a single place rather than across database environments.

That IBM got this message and got into the game isn’t surprising at all, but it is important. Lots of large companies buy IBM’s software.  If it wants them to follow it into the world of big data and Hadoop, it has to give them the tools they need to use it.

Wednesday, May 8, 2013

NuoDB Starlings Release 1.1


Today NuoDB announced the immediate availability of the NuoDB Starlings Release 1.1.  The new release features .NET & Windows 64-bit support helping developers who wish to avoid the common scalability and performance issues associated with traditional databases.  And that’s not all.  We’ve addressed three areas of customer need.

The new release delivers user benefit in 3 key areas:
Microsoft 64-bit and .NET support
Substantial performance and productivity enhancements
User experience improvements.

What’s New in 1.1

Microsoft Enhancements
 
64 bit support for Windows Server, Windows 7 and 8 for high performance

Full support for Visual Studio 2012, LINQ and Entity Framework for a more integrated developer experience

.NET driver to avoid the common scalability and performance issues associated with Microsoft SQL Server

Azure compatibility for running/deploying NuoDB easily in the cloud.

Productivity & Performance Improvements

33% increase in scale-out performance

20-50% improvement for heavy workloads

Built-in schema browsing and querying for Integrated SQL development
 
Database demand simulation tools, including a sample eCommerce schema and configurable load scenarios for improved provisioning of both predictable and unpredictable web traffic.

Improved User Experience

New NuoDB DevCenter– a central, easy to use location for all NuoDB developer resources, now integrated with the NuoDB Console for faster access

 SQL Explorer fully integrated into the NuoDB Console for dropping and creating web tables without ever leaving the browser.

 

Find All Databases name with Size

SELECT d.name,
ROUND(SUM(mf.size) * 8 / 1024, 0) Size_MBs
FROM sys.master_files mf
INNER JOIN sys.databases d ON d.database_id = mf.database_id
WHERE d.database_id > 4 -- Skip system databases
GROUP BY d.name
ORDER BY d.name

Friday, May 3, 2013

How Many Tables in a Database Having Column Name like ‘Name’.

USE AdventureWorks2008R2
GO

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%name%'
ORDER BY schema_name, table_name;

Select columns with NULL values only

declare @col varchar(255), @cmd varchar(max)
DECLARE getinfo cursor for
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID
WHERE t.Name = 'TABLE_1'
OPEN getinfo
FETCH NEXT FROM getinfo into @col
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM TABLE_1 WHERE [' + @col + '] IS NOT NULL) BEGIN print ''' + @col + ''' end'
    EXEC(@cmd)
    FETCH NEXT FROM getinfo into @col
END
CLOSE getinfo
DEALLOCATE getinfo

Thursday, May 2, 2013

Memory - Performance Counters

SQL Server provides numerous counters for monitoring and evaluating performance of your system. Although there are no hard and fast rules for performance monitoring, you should audit your system periodically to ensure that it lives up to user expectations. The first step in performance monitoring efforts should be collecting baseline information. A baseline has a collection of performance counter values while the system isn't heavily utilized and performs well. Next you should take benchmarks at various times of day and during different days of week to figure out acceptable ranges for the performance counters you intend to monitor. For example, your tests could reveal that buffer cache hit ratio values between 93.5% and higher are acceptable, but any value below 93.5% indicates sub-optimal performance.

Then setup continuous monitoring and define alerts which notify you if any performance counter values fall below (or rise above) a predefined threshold. Although we try to provide some guidelines for performance counter values adequate range for each counter will vary from one environment to next; this is why it's important to create a baseline and determine acceptable values for each counter.

You should watch several memory related counters very closely as they present a clear indication of acceptable or sub-optimal performance. Memory bottleneck typically leads to excessive paging. You can reduce or eliminate memory related performance issues by:

  1. Adding memory to the server computer.
  2. Altering SQL Server and operating system configuration.
  3. Eliminating unnecessary processes on the database server.
  4. Directing some user activity from the current server to another server.

A large majority of these memory performance counters are available through Memory Manager, Plan Cache and Buffer Manager objects.


SQL Server Buffer Manager provides the following counters:

AWE lookup maps / sec

Shows the number of times per second data pages requested by SQL Server were found in the buffer pool and then mapped as AWE memory and made part of server's virtual address space.


AWE stolen maps / sec

Shows the number of times data pages were taken from the free list in buffer pool and mapped as AWE memory.


AWE unmap calls / sec

When a buffer is unmapped it is excluded from virtual address space; buffers are unmapped because they haven't been used and to make room for new buffers. This counter shows the number of times buffers were un-mapped per second.


AWE unmap pages / sec

Shows the number of data pages unmapped per second.


AWE write maps / sec

Shows the number of times per second that it is necessary to map in a dirty buffer so it can be written to disk. This means that no free buffers are available so SQL Server has to write to a "dirty" buffer, meaning a buffer that contains data changes. If the value is high, then the server could use additional memory.


Note: AWE related counters are only available if AWE enabled option is turned on.

Buffer cache hit ratio

Percentage of pages that were found in the buffer pool without having to incur a read from disk. The ratio is calculated as total number of cache hits divided by the total number of cache lookups over the last few thousand page accesses. Since reading from the cache is much less expensive than reading from disk, servers that have a high buffer cache hit ratio perform well. For optimal performance this ratio should remain above 99%. However, in most environments a number above 95% is acceptable. If you use AWE enabled option you should never see this counter drop below 99% unless the total amount of RAM is not sufficient for your application's load.


Checkpoint pages per second

Shows how many pages are flushed per second by the CHECKPOINT process. If this value is too high, then you might want to increase the recovery interval or dedicate more RAM to SQL Server. By increasing the value of recovery interval, you advise SQL Server to checkpoint the databases less frequently thereby reserving more resources for user applications. The flip side of the coin, however, is that databases will take longer to recover in case of server failure (recovery process entails rolling back failed transactions and rolling forward the successful transactions).


Database pages

Number of pages in the buffer pool with database content. The higher the value of this counter, the more queries are sent to your server. If the number keeps increasing considerably above your baseline, consider allowing more memory for SQL Server usage by modifying the value of "max server memory" setting.


Free list stalls / sec

Number of times a request for a "free" page had to wait for one to become available. High value indicates that the server could use additional memory.


Lazy writes / sec

How many times per second lazy writer has to flush dirty pages out of the buffer cache instead of waiting on a checkpoint. Typically dirty pages are flushed out of cache by the checkpoint process. If the value of this counter is higher than 20, then server could use additional RAM.

Look at Lazy Writes/sec which tells you that number of times the buffer pool flushed dirty pages to disk outside of the CHECKPOINT process.  This should be near zero.



Page life expectancy

Shows on average how long each data page is staying in buffer cache before being flushed out to make room for other pages. If value is less than 300 seconds than additional memory could be helpful.


Page lookups / sec

Number of requests per second to find the page in the buffer pool.


Page reads / sec

Shows the number of data pages read per second.


Page writes / sec

Shows the number of data pages written per second.


Readahead pages / sec

Shows the number of data pages read per second in anticipation of their use.


Procedure cache pages

Number of pages used to store compiled queries; indicates the total number of stored procedures in cache.


Reserved pages

Shows the number of buffer pool reserved pages.


Stolen pages

Number of pages acquired from buffer cache and used for miscellaneous server purposes (including procedure cache).


Target pages

Shows the ideal number of pages in buffer pool.


Total pages

Total number of pages in buffer pool (including data pages, free and stolen pages).


Another group of important memory related counters are exposed through Cache Manager object in SQL Server 2000 and Plan Cache object with SQL Server 2005. These objects allow you to monitor the memory usage for storing ad-hoc Transact-SQL statements, stored procedures and triggers.


Cache Hit Ratio

Ratio between cache hits and lookups. The value of this counter should never fall below 90%.


Cache Object Counts

Shows the number of objects in cache.


Cache Pages

Number of 8KB pages in cache.


Cache Objects in use

Shows the number of objects in cache that are currently being used.


Each counter has the following instances:

  1. _total - information on all types of instances.
  2. SQL Plans - query plans for ad-hoc Transact-SQL statements, or Transact-SQL statements prepared using sp_prepare or sp_cursorprepare system procedures. The execution plans are cached for reuse by identical statements executed at a later time. With SQL Server 2000 this instance is called "ad hoc SQL Plans".
  3. Object Plans - query plans for stored procedures, user-defined functions and triggers. With SQL Server 2000 this instance is further broken down into procedure plans, replication procedure plans and trigger plans.
  4. Bound Trees - normalized trees for views, rules, computed columns and check constraints. With SQL Server 2000 this instance is called "misc. Normalized Trees".
  5. Extended stored procedures - if you use extended procedures this instance will show the number of these objects referenced in cache.
  6. Temporary tables & table variables - information for temp tables and table variables referenced in cache.


SQL Server Memory Manager object exposes the following counters:

Connection Memory

Total amount of dynamic memory the server is using for maintaining connections


Granted Workspace Memory

Total amount of memory currently granted to executing processes such as hash, sort, bulk copy, and index creation operations.


Lock Blocks

A lock block is a single locked object - a table, row, page, etc. This counter shows the total number of currently used lock blocks.


Lock Blocks Allocated

Current number of allocated lock blocks. By default SQL Server allocates locks dynamically as needed. However, you can override the default behavior by modifying locks configuration parameter.


Memory Grants Outstanding

Number of processes that have successfully acquired workspace memory grant. High value of this counter indicates peak user activity.


Memory Grants Pending

Number of processes waiting on a workspace memory grant. If this counter has a high value SQL Server could benefit from additional memory. You should first examine the database design, queries and indexes however to ensure the system is properly tuned before purchasing additional RAM.


Optimizer Memory

Memory reserved for query optimization. Ideally, the value of this counter should remain relatively static. If this isn't the case you might be using dynamic SQL execution excessively.


SQL Cache Memory

Total memory reserved for dynamic SQL statements.


Target Server Memory

Total amount of memory in KB that the server can consume.


Total Server Memory

The amount of committed memory in KB from the buffer pool. If this counter has a high values, as compared to the total amount of RAM available on server then SQL Server can benefit from additional memory.



The Target Server Memory counter shows SQL Server estimate of how much RAM it would need to operate effectively. The total memory counter shows what is actually used. The goal is to keep total memory less than target memory. If over time total memory number is higher than target your server could benefit from additional RAM.



In addition to examining SQL Server specific counters related to memory, you should also inspect the value of the following operating system level counters:

Total Server Memory (KB) counter  < Target Server Memory (KB) counter=That means  SQL Server has enough memory to run efficiently

Total Server Memory (KB) counter is >= Target Server Memory (KB) counter = This indicates that SQL Server may be under memory pressure and could use access to more physical memory.

Page faults / sec

Shows the average number of pages faulted per second. This counter includes both hard faults (those that require disk access) and soft faults (where the faulted page is found elsewhere in physical memory.) Most processors can handle large numbers of soft faults without significant consequence. However, hard faults, which require disk access, can cause significant delays. You need to ensure this value stays as low as possible.


Pages / sec

Shows the rate at which pages are read from or written to disk to resolve hard page faults. This counter is a primary indicator of the kinds of faults that cause system-wide delays. Although some spikes are normal the value of this counter should remain close to zero.

Look at Free Pages/sec and Free List Stalls/sec.  You don't want to see Free Pages bottom out which will result in a Free List Stall while the buffer pool has to free pages for usage.


Available Bytes

The amount of physical memory, in bytes, immediately available for allocation to a process or for system use. Your system should maintain at least 50MB of available memory. If the value falls below this threshold then the server is short on memory or the application (SQL Server) isn't releasing the memory back to the operating system as it should.


Process: Working Set

Shows the current size of the Working Set of the process in bytes. The Working Set is the set of memory pages touched recently by the threads in the SQL Server process. If the value of this counter is consistently below the amount configured by min server memory option then SQL Server is configured to have more memory than it really needs.

 

Tuesday, April 30, 2013

Cloudera’s CEO sizes up the SQL-on-Hadoop market

Cloudera’s Impala engine for interactive SQL queries on Hadoop data is now generally available, and CEO Mike Olson gives his lay of the competitive landscape.

There is no shortage of confidence in the Hadoop space, and market leader Cloudera bolstered its own on Tuesday with the general availability of its Impala SQL query engine for Hadoop. And if CEO Mike Olson’s comments are any indication, we’re in for a long ride of competitive jockeying and oneupmanship as Cloudera and its peers go all Microsoft or Google and create myriad new data-processing engines to turn their Hadoop distributions into bona fide platforms.
Launched as a private beta in May 2012 and made public in October, Impala is Cloudera’s attempt to address the growing demand for interactive SQL analytics on Hadoop data. It’s essentially a massively parallel database designed to share the same storage platform and metadata as Hadoop MapReduce, only it is its own separate processing engine.

Impala actually uses the same “nearly ANSI” version of SQL as does current standard bearer Hive, but that technology (created by Facebook in 2009 as a data warehouse layer for Hadoop) doesn’t run nearly fast enough to sate many users’ desire for interactive analytics. This is because Hive transforms SQL queries into MapReduce jobs, meaning every one is processed against the entire corpus of data in the Hadoop Distributed File System.





































Sizing up the competition

Only Cloudera isn’t the first to have the idea, nor is it alone in trying to sell interactive SQL on Hadoop. The idea was first commercialized by Boston-based startup Hadapt in 2011, and is now being pushed by numerous startups and larger Hadoop players. Among them: Pivotal (formerly EMC) Greenplum, MapR (with Drill), Hortonworks (with Stinger), Drawn to Scale, Splice Machine, Jethro Data and Citus Data.





But Cloudera is arguably the biggest name pushing SQL on Hadoop, and CEO Mike Olson thinks Impala stands out for several reasons — not the least of which is that it exists as a product. “Nobody else is shipping production-grade SQL query support on Hadoop,” he told me during a recent call. “At least not in open source.” He seems content to let the startups do their things, instead focusing his attention on Cloudera’s big three Hadoop-distribution competitors in Pivotal, MapR and Hortonworks. Greenplum and Pivotal SVP Scott Yara was full of confidence — and R&D budget– when the company announced the Pivotal HD distribution and HAWQ technology in February, but Olson claims the approach requires a siloed DBMS within HDFS and is a “rearguard defensive strategy” to protect the company’s sunk costs in its database technology.




As for Hortonworks, Olson questions the wisdom of its Stinger initiative to boost Hive’s speed, noting that “Hive never got good while it was running standalone on MapReduce.” Hortonworks also partners with vendors such as Teradata to let their platforms access Hadoop data in its native format, but those approaches still require sending data over the network. “It’s not the way you would build it if you woke up in the 2000s and were building this anew,” Olson said.






Olson acknowledged that the MapR-led Apache Drill project is cut from the same cloth as Impala (that is, being a Google Dremel clone designed specifically for Hadoop), but “the difference is we’re shipping code.” Being generally available and ready for production workloads means Cloudera can lock down users and market share before many even have a chance to experiment with Drill. He all but dismissed questions over the readiness of Impala, spurred by rumblings in the Hadoop space that Cloudera rushed it into public beta in order to get on the scoreboard against more fully baked offerings.
“I don’t feel we’re under the gun competitively to pull it out of beta because no one else has product in the market,” Olson said. “I have no problems … calling this GA quality.” He did, however, acknowledge that Impala is shipping with a “minium viable feature set” that the company has plans to build on in the near future. Impala Senior Product Manager Justin Erickson noted a few issues of concern, including around the number of concurrent users Impala can support, but said they have been addressed during the beta period.

One piece of a larger platform

Really, though, the whole point of Impala and its competitors is to turn Hadoop from a tool for batch analytics and mass storage into a platform that can handle nearly all of companies’ data-processing needs. In that regard, it appears we’re just getting started. Cloudera, MapR, Pivotal Greenplum and Hortonworks are already pushing their own products and projects, and Olson said “it’s absolutely our intent” to enhance Cloudera’s platform with even more open-source products — perhaps even more database technologies a la HBase — that will let users do more stuff with more types of data. Over time, this strategy could result in Hadoop displacing the current breed of databases and data warehouses and becoming the single data store atop of which users run whatever applications they so desire. For now, though, especially when it comes to Impala and the data warehouse incumbents, Olson is taking a measured approach. “The likelihood that we’re going to knock them off in the near term,” he said, “… it would be a tough fight to win.”

 

TSQL command to check if SQL Agent services is started or down.

SET
NOCOUNT ON

DECLARE
@ISCOUNT varchar(100)

DECLARE
@ISCOUNT2 integer

BEGIN


SELECT @ISCOUNT2 = count(1)

FROM master.sys.sysprocesses

WHERE program_name like N'%SQLAgent%'

IF @ISCOUNT2 < 0

BEGIN

SET @ISCOUNT = convert(varchar(100),'FAILURE: SQL Server Agent service is NOT started!')

END

ELSE

BEGIN

SET @ISCOUNT = convert(varchar(100),'SQL Server Agent service is started.')

END

PRINT @ISCOUNT

END

GO

Monday, April 29, 2013

Oracle Unveils SPARC Servers with the World’s Fastest Microprocessor

New SPARC Servers Redefine the Economics of Enterprise Computing: Deliver Extreme Performance and Value for Database and Enterprise Applications, Trump the Competition on Multiple Business-Critical Workloads

Redwood Shores, Calif. – March 26, 2013

News Facts

Oracle today announced a complete refresh of their midrange and high-end SPARC server lines with new SPARC T5 and M5 servers running Oracle Solaris.
Oracle’s new SPARC T5 midrange servers are based on the SPARC T5, the world’s fastest microprocessor.
Businesses today need the computing power to exploit Big Data to maximum advantage, to use analytics to discover growth and opportunities, and turn social streams into market intelligence. Traditional systems simply don’t have the horsepower to handle those demanding tasks.
Oracle’s new SPARC T5 servers have set 17 world records(1) and are the world’s best platforms for enterprise computing at any scale, delivering the best value for database and enterprise applications.
Oracle’s SPARC T5-8 is the fastest single server for Oracle Database(2).
Oracle’s SPARC T5-8 is also the fastest single server for Oracle Middleware with a 12x cost performance advantage over the IBM Power 780(3).
Oracle’s SPARC T5-8 server has a 2.5x price performance advantage over the IBM P780 3-node cluster, when including hardware and software costs(4).
Oracle's SPARC T5-8 server has a 7x price advantage over a similar IBM Power 780 configuration for database on a server-to-server basis(5).
 

Friday, April 19, 2013

Get Top Queries Tiring SQL Server CPUs


use tempdb
go
IF object_id('tempdb..##FindTopCPUQueries_set1') is not null DROP TABLE [dbo].[##FindTopCPUQueries_set1]
GO
declare @ServerTime datetime = getdate()
, @ConvertMiliSeconds bigint = 1000
, @FilterMoreThanMiliSeconds bigint = 1
, @FilterHours bigint = 2
, @execution_count bigint = 2
, @debugFlg bit = 0
if @debugFlg=1 select @ServerTime as ServerTime, @ConvertMiliSeconds as ConvertMiliSeconds
, @FilterMoreThanMiliSeconds as FilterMoreThanMiliSeconds, @FilterHours as FilterHours
, @execution_count as execution_count
select TOP 300
@@servername as servername,@ServerTime as runtime
,isnull(db_name(QueryText.dbid),'PreparedSQL') as DBName
 ,SUBSTRING(QueryText.text, (QueryStats.statement_start_offset/2)+1,
(isnull((
CASE QueryStats.statement_end_offset
 WHEN -1 THEN DATALENGTH(QueryText.text)
 WHEN 0 THEN DATALENGTH(QueryText.text)
 ELSE QueryStats.statement_end_offset
 END - QueryStats.statement_start_offset),0)/2)
 + 1) AS QueryExecuted
,total_worker_time AS total_worker_time
,QueryStats.execution_count as execution_count
,statement_start_offset,statement_end_offset
,(case when QueryText.dbid is null then OBJECT_NAME(QueryText.objectid) else OBJECT_NAME(QueryText.objectid, QueryText.dbid) end) as ObjectName
,query_hash
,plan_handle
,sql_handle
into ##FindTopCPUQueries_set1
from sys.dm_exec_query_stats as QueryStats
cross apply sys.dm_exec_sql_text(QueryStats.sql_handle) as QueryText
where QueryStats.query_hash IN
(
select QueryStatsBaseTable.query_hash
from sys.dm_exec_query_stats QueryStatsBaseTable
where last_execution_time > DATEADD(hh,-@FilterHours,GETDATE())
group by query_hash
having (sum(total_worker_time)/sum(execution_count))>@ConvertMiliSeconds and sum(execution_count)>@execution_count
)
ORDER BY total_worker_time/execution_count DESC;
if @debugFlg=1 select * from ##FindTopCPUQueries_set1 order by QueryExecuted
IF object_id('tempdb..##FindTopCPUQueries_set2') is not null DROP TABLE [dbo].[##FindTopCPUQueries_set2]
select
servername,runtime,max(DBName) as DBName,max(QueryExecuted) as QueryExecuted,(sum(total_worker_time)/sum(execution_count))/@ConvertMiliSeconds as AvgCPUTime
,sum(execution_count) as execution_count,query_hash, max(ObjectName) as ObjectName
into ##FindTopCPUQueries_set2
from ##FindTopCPUQueries_set1
group by query_hash,servername,runtime
order by AvgCPUTime desc
select * from ##FindTopCPUQueries_set2
--where QueryExecuted like 'select TOP 300%'
order by AvgCPUTime desc

Login Password Policy

Outputs a list of all the SQL logins and for each one indicates if the password policy in NOT enforced or if the password IS enforced, is the password expiration also enforced.

SET NOCOUNT ON
SELECT
name,
CASE CAST(is_policy_checked AS TINYINT) + CAST(is_expiration_checked AS TINYINT)
WHEN 0 THEN 'Not Enforced'
WHEN 1 THEN 'Password - No Expiration'
WHEN 2 THEN 'Password With Expiration'
END AS PasswordEnforcement ,
LOGINPROPERTY(name,'BadPasswordCount') AS BadPasswordCount,
LOGINPROPERTY(name,'BadPasswordTime') AS BadPasswordTime,
LOGINPROPERTY(name,'DaysUntilExpiration') AS DaysUntilExpiration,default_database_name,
CASE WHEN LOGINPROPERTY(name,'IsExpired') = 0 THEN 'NO' ELSE 'YES' END AS IsExpired,
CASE WHEN LOGINPROPERTY(name,'IsLocked') = 0 THEN 'NO' ELSE 'YES' END AS IsLocked,
CASE WHEN LOGINPROPERTY(name,'IsMustChange') = 0 THEN 'NO' ELSE 'YES' END AS IsMustChange,
LOGINPROPERTY(name,'LockoutTime') AS LockoutTime,
LOGINPROPERTY(name,'PasswordLastSetTime') AS PasswordLastSetTime
FROM sys.sql_logins
ORDER BY name

Get space used by queries in tempdb

USE tempdb
Go
SELECT t.text,
      SUM(internal_objects_alloc_page_count) AS task_internal_objects_alloc_page_count,
      SUM(internal_objects_dealloc_page_count) AS task_internal_objects_dealloc_page_count
    FROM sys.dm_db_task_space_usage  st
 JOIN sys.sysprocesses sp
  ON sp.spid = st.session_id
 CROSS APPLY sys.dm_exec_sql_text(sp.sql_handle) t
    GROUP BY t.text
 ORDER BY 2 DESC

How much space could you get shrinking all databases

This is script is useful when you are short of space on a drive and you need to know how much space can you get shrinking your databases. Although is shrinking is not a recommended practice some times you need to get free space in your drive. Just execute the script and it will tell you the free space you can obtain.

You gan modify the line where[UsageType] = 'data' to specify log or data files.

As well you can uncomment the line --where MB_Free < 5000 to specify drives with less than 'x mbytes'.


begin
   
    set nocount on
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#dbfileinfo'))
      begin
          drop table #dbfileinfo
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#logsizestats'))
      begin
          drop table #logsizestats
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#datafilestats'))
      begin
          drop table #datafilestats
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#fixeddrives'))
      begin
          drop table #fixeddrives
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#usados'))
      begin
          drop table #usados
      end
    create table #fixeddrives
      (
         DriveLetter varchar(10),
         MB_Free     dec(20, 2)
      )
    create table #datafilestats
      (
         DBName       varchar(255),
         DBId         int,
         FileId       tinyint,
         [FileGroup]  tinyint,
         TotalExtents dec(20, 2),
         UsedExtents  dec(20, 2),
         [Name]       varchar(255),
         [FileName]   varchar(400)
      )
    create table #logsizestats
      -- DBCC SQLPERF -- Provides statistics about how the transaction-log space was used in all databases. It can also be used to reset wait and latch statistics.
       (
         DBName      varchar(255) not null primary key clustered,
         -- Database Name -- Name of the database for the log statistics displayed.
          DBId        int,
         LogFile     real,
         -- Log Size (MB) -- Actual amount of space available for the log. This amount is smaller than the amount originally allocated for log space because the SQL Server 2005 Database Engine reserves a small amount of disk space for internal header information.
          LogFileUsed real,
         -- Log Space Used (%) -- Percentage of the log file currently occupied with transaction log information.
          Status      bit
      ) -- Status -- Status of the log file. Always 0.
    create table #dbfileinfo
      (
         [ServerName]        varchar(255),
         [DBName]            varchar(65),
         [LogicalFileName]   varchar(400),
         [UsageType]         varchar (30),
         [Size_MB]           dec(20, 2),
         [SpaceUsed_MB]      dec(20, 2),
         [MaxSize_MB]        dec(20, 2),
         [NextAllocation_MB] dec(20, 2),
         [GrowthType]        varchar(65),
         [FileId]            smallint,
         [GroupId]           smallint,
         [PhysicalFileName]  varchar(400),
         [DateChecked]       datetime
      )
    declare @SQLString varchar(3000)
    declare @MinId int
    declare @MaxId int
    declare @DBName varchar(255)
    declare @tblDBName table (
      RowId  int identity(1, 1),
      DBName varchar(255),
      DBId   int)
    insert into @tblDBName
                (DBName,
                 DBId)
    select [Name],
           DBId
    from   master..sysdatabases
    where  ( Status & 512 ) = 0 /*NOT IN (536,528,540,2584,1536,512,4194841)*/
    order  by [Name]
    insert into #logsizestats
                (DBName,
                 LogFile,
                 LogFileUsed,
                 Status)
    exec ('dbcc sqlperf(logspace) with no_infomsgs')
    update #logsizestats
    set    DBId = db_id(DBName)
    insert into #fixeddrives
    exec master..xp_fixeddrives
    select @MinId = min(RowId),
           @MaxId = max(RowId)
    from   @tblDBName
    while ( @MinId <= @MaxId )
      begin
          select @DBName = [DBName]
          from   @tblDBName
          where  RowId = @MinId
          select @SQLString = 'SELECT ServerName = @@SERVERNAME,' +
                              ' DBName = ''' +
                              @DBName +
                                     ''',' +
                                                  ' LogicalFileName = [name],' +
       ' UsageType = CASE WHEN (64&[status])=64 THEN ''Log'' ELSE ''Data'' END,'
                    +
                          ' Size_MB = [size]*8/1024.00,' +
                   ' SpaceUsed_MB = NULL,' +
-- 20081125 Arithmetic overflow error converting expression to data type int.
-- ' MaxSize_MB = CASE [maxsize] WHEN -1 THEN -1 WHEN 0 THEN [size]*8/1024.00 ELSE maxsize*8/1024.00 END,'+
 ' MaxSize_MB = CASE [maxsize] WHEN -1 THEN -1 WHEN 0 THEN [size]*8/1024.00 ELSE maxsize/1024.00*8 END,'
 +
-- 20081125 end
' NextExtent_MB = CASE WHEN (1048576&[status])=1048576 THEN ([growth]/100.00)*([size]*8/1024.00) WHEN [growth]=0 THEN 0 ELSE [growth]*8/1024.00 END,'
 +
' GrowthType = CASE WHEN (1048576&[status])=1048576 THEN ''%'' ELSE ''Pages'' END,'
 + ' FileId = [fileid],' + ' GroupId = [groupid],' +
             ' PhysicalFileName= [filename],' +
             ' CurTimeStamp = GETDATE()' +
-- 20081125 begin @DBName embedded spaces
-- 'FROM '+@DBName+'..sysfiles' 
'FROM [' + @DBName + ']..sysfiles'
    -- 20081125 end
    print @SQLString
    insert into #dbfileinfo
    exec (@SQLString)
    update #dbfileinfo
    -- 20081125 begin LogFileUsed is %
    -- SET SpaceUsed_MB = (SELECT LogFileUsed FROM #LogSizeStats WHERE DBName = @DBName)
     set    SpaceUsed_MB = Size_MB / 100.0 * (select LogFileUsed
                                             from   #logsizestats
                                             where  DBName = @DBName)
    -- 20081125 end
    where  UsageType = 'Log'
           and DBName = @DBName
    -- 20081125 begin @DBName embedded spaces
    -- SELECT @SQLString = 'USE ' + @DBName + ' DBCC SHOWFILESTATS WITH NO_INFOMSGS'
     select @SQLString = 'USE [' + @DBName +
                        '] DBCC SHOWFILESTATS WITH NO_INFOMSGS'
    -- 20081125 end
    insert #datafilestats
           (FileId,
            [FileGroup],
            TotalExtents,
            UsedExtents,
            [Name],
            [FileName])
    execute(@SQLString)
    update #dbfileinfo
    set    [SpaceUsed_MB] = S.[UsedExtents] * 64 / 1024.00
    from   #dbfileinfo as F
           inner join #datafilestats as S
             on F.[FileId] = S.[FileId]
                and F.[GroupId] = S.[FileGroup]
                and F.[DBName] = @DBName
    truncate table #datafilestats
    select @MinId = @MinId + 1
end
    select @@servername                            as servidor,
           substring(A.PhysicalFileName, 1, 1)     as unidad,
           sum ([Size_MB])                         as SqlTotalDB,
           sum([SpaceUsed_MB])                     as SqlTotalUsedSpaceDB,
           sum (( [Size_MB] ) - ( [SpaceUsed_MB] ))as SQLTotalFreeSpaceDB
    into   #usados
    from   #dbfileinfo as A
           left join #fixeddrives as B
             on substring(A.PhysicalFileName, 1, 1) = B.DriveLetter
    --ORDER BY CAST([NextAllocation_MB] AS decimal(6,2)) 
    --ORDER BY [FreeSpaceInDrive] 
    --where ([Size_MB] - [SpaceUsed_MB]) -  ([NextAllocation_MB]) < 0
    --where (B.MB_Free) + (([Size_MB] - [SpaceUsed_MB]) -  ([NextAllocation_MB])) < 2000
     --where (B.MB_Free) + (([Size_MB] - [SpaceUsed_MB]) -  ([NextAllocation_MB])) <= 0
     --where CONVERT(sysname,DatabasePropertyEx([DBName],'Recovery')) = 'FULL'
    --where [MaxSize_MB] <> 2097152.00 and [MaxSize_MB] <> -1.00
    --where left ([PhysicalFileName],1) = 'C'
    --where (CAST(([Size_MB] - [SpaceUsed_MB]) / [Size_MB] AS decimal(4,2))) > 0.5 and ([Size_MB]) > 5000 --mas de la mitad de espacio libre
     --where ([Size_MB]) > 100000 --TAMA?O DE LA BASE DE DATOS
    --where [DBName] like '%content%'
    where[UsageType] = 'data'
    --where [NextAllocation_MB] < 5 and [SpaceUsed_MB] > 20 and [dbname] not in ('master','model','msdb','tempdb') -- bases de datos grandes con poco crecimiento
     --where [NextAllocation_MB] > 500 -- bases de datos con crecimiento desmesurado
     --where [Size_MB] - [SpaceUsed_MB] < 50 and [UsageType] = 'log'
    group  by substring(A.PhysicalFileName, 1, 1)
   
    select
           sum(SQLTotalFreeSpaceDB)
    
    from   #fixeddrives as f
           inner join #usados as z
             on z.unidad = f.DriveLetter
   
 --where MB_Free < 5000   

    --select * from #usados
    --ORDER BY ([Size_MB] - [SpaceUsed_MB]) -  ([NextAllocation_MB])
    --ORDER BY CAST([NextAllocation_MB] AS decimal(6,2)) 
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#dbfileinfo'))
      begin
          drop table #dbfileinfo
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#logsizestats'))
      begin
          drop table #logsizestats
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#datafilestats'))
      begin
          drop table #datafilestats
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#fixeddrives'))
      begin
          drop table #fixeddrives
      end
    if exists (select 1
               from   tempdb..sysobjects
               where  [Id] = object_id('tempdb..#usados'))
      begin
          drop table #usados
      end
    -- 20081125 begin SET NOCOUNT OFF
    set nocount off
-- 20081125 end
end 

List Database Users and their Corresponding Roles

This script will generate list of users and their corresponding database, application roles and type of login for a given database.

WITH Roles_CTE(Role_Name, Username)
AS
(
 SELECT
  User_Name(sm.[groupuid]) as [Role_Name],
  user_name(sm.[memberuid]) as [Username]
 FROM [sys].[sysmembers] sm
)
SELECT 
    Roles_CTE.Role_Name,
    [DatabaseUserName] = princ.[name],
    [UserType] = CASE princ.[type]
                    WHEN 'S' THEN 'SQL User'
                    WHEN 'U' THEN 'Windows User'
                    WHEN 'G' THEN 'Windows Group'
                    WHEN 'A' THEN 'Application Role'
                    WHEN 'R' THEN 'Database Role'
                    WHEN 'C' THEN 'User mapped to a certificate'
                    WHEN 'K' THEN 'User mapped to an asymmetric key'
                 END
FROM
    sys.database_principals princ
JOIN Roles_CTE on Username = princ.name
where princ.type in ('S', 'U', 'G', 'A', 'R', 'C', 'K')
ORDER BY princ.name

Get Detach or Attach all user databases script

If you want detach script, run the above script with @DetachOrAttach = 1. Run with 0 for attach script.

Script will print detach script or attach script for all user databases. You can copy the script from the "messages" screen in ssms and execute to detach/attach. If you want to detach or attach specific databases you can modify the cursor sql accordingly.

USE [master];
GO
DECLARE @database NVARCHAR(200) ,
    @cmd NVARCHAR(1000) ,
    @detach_cmd NVARCHAR(4000) ,
    @attach_cmd NVARCHAR(4000) ,
    @file NVARCHAR(1000) ,
    @i INT ,
    @DetachOrAttach BIT;
SET @DetachOrAttach = 1;
-- 1 Detach 0 - Attach
-- 1 Generates Detach Script
-- 0 Generates Attach Script
DECLARE dbname_cur CURSOR STATIC LOCAL FORWARD_ONLY
FOR
    SELECT  RTRIM(LTRIM([name]))
    FROM    sys.databases
    WHERE   database_id > 4;
 -- No system databases
OPEN dbname_cur
FETCH NEXT FROM dbname_cur INTO @database
WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT  @i = 1;
        SET @attach_cmd = '-- ' + QUOTENAME(@database) + CHAR(10)
            + 'EXEC sp_attach_db @dbname = ''' + @database + '''' + CHAR(10);
      -- Change skip checks to false if you want to update statistics before you detach.
        SET @detach_cmd = '-- ' + QUOTENAME(@database) + CHAR(10)
            + 'EXEC sp_detach_db @dbname = ''' + @database
            + ''' , @skipchecks = ''true'';' + CHAR(10);
      -- Get a list of files for the database
        DECLARE dbfiles_cur CURSOR STATIC LOCAL FORWARD_ONLY
        FOR
            SELECT  physical_name
            FROM    sys.master_files
            WHERE   database_id = DB_ID(@database)
            ORDER BY [file_id];
        OPEN dbfiles_cur
        FETCH NEXT FROM dbfiles_cur INTO @file
        WHILE @@FETCH_STATUS = 0
            BEGIN
                SET @attach_cmd = @attach_cmd + '    ,@filename'
                    + CAST(@i AS NVARCHAR(10)) + ' = ''' + @file + ''''
                    + CHAR(10);
                SET @i = @i + 1;
                FETCH NEXT FROM dbfiles_cur INTO @file
            END
        CLOSE dbfiles_cur;
        DEALLOCATE dbfiles_cur;
        IF ( @DetachOrAttach = 0 )
            BEGIN
            -- Output attach script
                PRINT @attach_cmd;
            END
        ELSE -- Output detach script
            PRINT @detach_cmd;
        FETCH NEXT FROM dbname_cur INTO @database
    END
CLOSE dbname_cur;
DEALLOCATE dbname_cur;