Can I optimize SQL selection in fn_listextendedproperty?

I have a C # project that launches several requests at startup. I tried to find out if I can optimize the runtime. Other requests take less than 100 ms, but this request is a bit slower.

SELECT * FROM 
  fn_listextendedproperty(default, default, default, default, default, default, default) 
  where name = 'CUSTOM_EX_PROP'

301 ms

This is normal? Can this query be optimized? Is there a faster way to read an extended database property?

Here is my C # code in case

var watch = System.Diagnostics.Stopwatch.StartNew ();
using (SqlDataAdapter sda = new SqlDataAdapter (new SqlCommand (query, _con))) {
    sda.Fill (dt);
}
watch.Stop ();
var elapsedMs = watch.ElapsedMilliseconds;
System.Diagnostics.Debug.Print (query + "\r\n" + elapsedMs.ToString () + " ms");

EDIT. As @Liam pointed out that it smells like an XY problem, let me tell you about a real scenario. The database version is stored in an extended property, and when I connect to the database, I just want to make sure the version is right. That is why I read its extended property at startup.

+4
2

fn_listextendedproperty , ... ,

 insert @ids select object_id, name from sys.objects  
  where schema_id = @major  
  and parent_object_id = 0  
  and 0 <> charindex( '.'+type+'.',  
   case @level1type  
    when 'TABLE' then '.U .'  
    when 'VIEW' then '.V .'  
    when 'RULE' then '.R .'  
    when 'DEFAULT' then '.D .'  
    when 'QUEUE' then '.SQ.'  
    when 'SYNONYM' then '.SN.'  
    when 'AGGREGATE' then '.AF.'  
    when 'FUNCTION' then '.TF.FN.IF.FS.FT.'  
    when 'PROCEDURE' then '.P .PC.RF.X .'  
    when 'SEQUENCE' then '.SO.'  
    end )  
end  

-- Now get properties from id-s obtained, and return  
--  
insert @tab select @basetype, i.nam, p.name, p.value  
 from sys.extended_properties p join @ids i on p.class = @class and p.major_id = i.maj  
 where p.minor_id = 0 and (@name is null or @name = p.name)  

, .

select * from 
sys.extended_properties
+1

;

, .

,

exec sp_helptext 'fn_listextendedproperty'
0

Source: https://habr.com/ru/post/1682921/


All Articles