Вопрос

A. Is there the possibility to get the result of ps-script:

$ss=New-Object Microsoft.SqlServer.Management.Smo.Server 'MyServer';
$ss.Databases[$db].Tables |  Where-Object  {$_.IsIndexable -eq $true}

with the comparable performance to the select

SELECT * FROM sys.Tables WHERE OBJECTPROPERTY(object_id, 'IsIndexable')=1

Of course one way is to run select (from ps) and then instantiate the bunch of smo.table by names (or object_id), something like:

$d = $ss.Databases[$db]
$r = $d.ExecuteWithResults('SELECT object_id FROM sys.tables WHERE OBJECTPROPERTY(object_id, ''IsIndexable'')=1');
$t = @()
foreach ($i in $r.Tables[0].Rows) {$t+=$d.Tables.ItemById($i["object_id"])}

but this is doesn't look "how it should be"..

B. How to improve performance of indexes filter?

$i=$d.Tables.Indexes | where {$_.HasFilter -eq $true};

Of course the same result is possible to achieve quick just but sql:

$d = $ss.Databases[$db]
$r = $d.ExecuteWithResults('SELECT object_id, Name FROM sys.indexes WHERE has_filter=1 ORDER BY object_id, Name');
$t = @()
foreach ($i in $r.Tables[0].Rows) {$t+=$d.Tables.ItemById($i["object_id"]).Indexes[$i["Name"]]}

but I think there should be better solution..

Это было полезно?

Решение

One thing that will speed up your code is to tell the server object that you care about the IsIndexable property on tables, so when you fetch a table object it should grab that property. You can follow the process on this BOL article, but it boils down to:

$ss=New-Object Microsoft.SqlServer.Management.Smo.Server 'MyServer';
$tableType = (new-object Microsoft.SqlServer.Management.Smo.Table).getType();
$ss.SetDefaultInitFields($tableType, "IsIndexable");
$ss.Databases[$db].Tables |  Where-Object  {$_.IsIndexable -eq $true}

Note that in the pointed to BOL article, they save off the default set of properties and restore it when they're done. Whether you care to do that or not is, well, up to you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top