Why is the execution speed too slow when the Oracle database Guid is used as the primary key?

Source: Internet
Author: User
Tags mscorlib

Oracle DatabaseWhat is the reason for the slow execution of GUID as the primary key? Next we will start to introduce it. First, introduce the environment. The database uses Oracle10g. All character fields are varchar2 [1]. All primary keys use Guid, which is of the varchar2 (36) type in the database. Corresponding, the Object Id attribute is of the string type. The ORM usesNhib1_2.1.0And FluentNHibernate1.1.

After some troubleshooting, we found that the root cause of the problem was that when NH passed the SQL statement to Oracle, all struct parameters were of the nvarchar2 type, while the corresponding fields in the database were of the varchar2 type, this will cause Oracle to fail to use the index, and finally cause full table scanning. Therefore, if the data volume is a little large, it will not slow down.

The first solution is to change the type of all the reserved fields in the database from varchar2 to nvarchar2. We do not want to do this for various reasons.

The second solution is to let NH pass varchar2 as the parameter type to Oracle.

In fact, NH maps the. net string to DbType. String [2] by default, and maps DbType. String to nvarchar2 [3]. Map DbType. AnsiString to varchar2 [4].

Therefore, the query is simple. You only need to specify the HQL parameter type as AnsiString.

 
 
  1. var query = Session.CreateQuery(@"select t from Region as t  
  2.  
  3. where t.Id = :Id")  
  4.  
  5. .SetAnsiString("Id", id);  
  6.  
  7. var query = Session.CreateQuery(@"select t from Region as t  
  8.  
  9. where t.Id in (:Ids)")  
  10.  
  11. .SetParameterList("Ids", ids.ToList(), NHibernateUtil.AnsiString); 

But how do I set the parameter types of the Update and Delete statements? Here is a small secret to specify the attribute type in the ing file as "AnsiString.

 
 
  1. public class RegionMap : TreeNodeMap<Region> 
  2.  
  3. {  
  4.  
  5. public RegionMap()  
  6.  
  7. {  
  8.  
  9. Table("INFRA_REGION");  
  10.  
  11. Id(t => t.Id, "REGION_ID").CustomType("AnsiString");  
  12.  
  13. ...  
  14.  
  15. }  
  16.  

Note: You must use CustomType () instead of CustomSqlType ().

Of course, it would be annoying to change every configuration file. It seems that the project uses Fluent nhib.pdf and you only need to add an IdConvention.

 
 
  1. public class IdConvention : FluentNHibernate.Conventions.IIdConvention  
  2.  
  3. {  
  4.  
  5. public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)  
  6.  
  7. {  
  8.  
  9. instance.CustomType("AnsiString");  
  10.  
  11. }  
  12.  

To be thorough, you can add a convention of the string type property.

 
 
  1. public class StringPropertyConvention : IPropertyConvention, IPropertyConventionAcceptance  
  2.  
  3. {  
  4.  
  5. public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)  
  6.  
  7. criteria.Expect(x => x.Property.PropertyType == typeof(string));  
  8.  
  9. public void Apply(IPropertyInstance instance)  
  10.  
  11. {  
  12.  
  13. instance.CustomType("AnsiString");  
  14.  
  15. }  
  16.  

Add the two Convention to the Configuration:

 
 
  1. Session ["SessionFactory"] = Fluently. Configure ()
  2.  
  3. . Database (OracleClientConfiguration. Oracle10
  4.  
  5. . Dialect <Oracle10gDialect> ()
  6.  
  7. . ConnectionString ("User ID = iBlast; Password = not allowed; Data Source = Moki ")
  8.  
  9. . QuerySubstitutions ("true 1, false 0, yes 'y', no 'n '")
  10.  
  11. . UseOuterJoin ()
  12.  
  13. . ProxyFactoryFactory <ProxyFactoryFactory> ()
  14.  
  15. . AdoNetBatchSize (1000)
  16.  
  17. . Driver <OracleClientDriver> ())
  18.  
  19. . Mappings (m => {m. HbmMappings. AddFromAssembly (Assembly. Load ("Infrastructure. Repositories "));
  20.  
  21. M. FluentMappings. AddFromAssembly (Assembly. Load ("Infrastructure. Repositories "))
  22.  
  23. . Conventions. Add <EnumConvention> ()
  24.  
  25. . Conventions. Add <HasManyConvention> ()
  26.  
  27. . Conventions. Add <HasManyToManyConvention> ()
  28.  
  29. . Conventions. Add <StringPropertyConvention> ()
  30.  
  31. . Conventions. Add <IdConvention> ()
  32.  
  33. . ExportTo (@ "F: \ temp \");})
  34.  
  35. . BuildSessionFactory ();

Note the last and second rows. exportTo (@ "F: \ temp \") outputs the ing file to "F: \ temp \" to test whether the generated ing file is correct \", the ing file should look like this:

 
 
  1.  
  2. <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-update="true" mutable="true" where="IsDelete=0" name="Dawn.HIS.Infrastructure.Core.Data.Region, Infrastructure.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="INFRA_REGION"> 
  3.  
  4. <id name="Id" type="AnsiString"> 
  5.  
  6.   <column name="REGION_ID" /> 
  7.  
  8.   <generator class="assigned" /> 
  9.  
  10. </id> 
  11.  
  12. <version name="Version" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
  13.  
  14.   <column name="Version" /> 
  15.  
  16. </version> 
  17.  
  18. <property name="CreateTime" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
  19.  
  20.   <column name="CREATETIME" /> 
  21.  
  22. </property> 
  23.  
  24. <property name="Name" type="AnsiString"> 
  25.  
  26.   <column name="NAME" /> 
  27.  
  28. </property> 
  29.  
  30. ...  
  31.  
  32. </class> 
  33.  

Note:

[1] varchar2 instead of nvarchar2 is used to avoid performance problems in nvarchar2 sorting, in addition to considering varchar2 to save space.

[2] See NHibernate-2.1.0.GA-src \ src \ nhib.pdf \ Type \ TypeFactory. cs line 197th.

[3] See NHibernate-2.1.0.GA-src \ src \ nhibect \ Dialect \ Oracle8iDialect. cs line 92nd.

[4] See NHibernate-2.1.0.GA-src \ src \ nhibect \ Dialect \ Oracle8iDialect. cs line 88th.

This article will introduce you here. If you want to learn more about Oracle databases, you can read this article: http://database.51cto.com/oracle /.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.