WQL does not support TOP - need workaround

霸气de小男生 提交于 2019-12-01 17:49:52

问题


WQL (basically SQL for WMI) does not support a TOP or LIMIT keyword. Sql Server used TOP and many other RDBMSs supprt LIMIT etc.

Is there a workaround to emulating a SELECT query to behave as though it had a TOP/LIMIT clause that limits the result set to some arbitrary number?

Or is there some other WQL-specific keyword that works like TOP or LIMIT?


回答1:


Nope, there's no way to simulate TOP using WQL alone.

Exception: if you're lucky enough to be querying a WMI class which has ungapped, ascending numeric instance numbers used as keys, then you can use greater-than and less-then comparisons to limit and page through the results.

It's possible that ManagementClass.GetInstances() instead of using a WQL query might allow you to cancel the enumeration midway once you've collected enough instances, and hence avoid paying the CPU and RAM cost of enumerating the whole list at once.

Note that, AFAIK, the CIMV2 WMI provider doesn't natively handle WQL-- instead it simply relies on WMI to enumerate all instances, process the WQL, and filter the results before returning them to the caller. But the expensive part (actually fetching the underlying WMI data) is still done. So I believe there's no efficiency gain to be had (for local WMI queries, that is) by using WQL vs. using GetInstances() and filtering the results yourself-- and if GetInstances() allows you to cancel midway, then GetInstances() may be much cheaper for long result sets.




回答2:


pipe it with "select xyz -First 1"

eg: Get-WmiObject win32_logicaldisk |select -First 1




回答3:


Like Justin has said. I am not sure about your exact requirement though. I was doing a simple project with Visual Basic and part of it was to fetch event log, the listview fails due to the huge size of Application log(>20MB) and the control enters into some kind of infinite loop. I wanted to limit so here is the pseudocode

topval = UserInputText.Text 'Assuming user entered the top 10 or 20

while ThereAreStillItemsInCollection
 if topval = 0 then
 goto CleanUpMemObjectsAndReturn
 end if
topval = topval  - 1
wend
CleanUpMemObjectsAndReturn:
set obj = Nothing
end sub


来源:https://stackoverflow.com/questions/1574336/wql-does-not-support-top-need-workaround

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!