These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Index for column
PostPosted: Thu Jun 12, 2008 5:39 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
Hi,

I need to create an indexed string column.

I don't find the mapping to use. Can you help me please ?

Code:
    <property name="Last_name"   column="last_name" access="field.pascalcase-underscore" not-null="false" type="System.String"  insert="true" update="true"/>


Thanks for your help !

J-L


Top
 Profile  
 
 Post subject: Re: Index for column
PostPosted: Fri Jun 13, 2008 1:06 am 
Newbie

Joined: Wed May 28, 2008 3:17 am
Posts: 10
Papy214 wrote:
Hi,

I need to create an indexed string column.
I don't find the mapping to use. Can you help me please ?
<snip>
J-L


Hi J-L

Indexes are created on the database to help speed up queries. Mostly you just create the index and allow the db to use it. You don't have to do anything else after creating it. Sometimes, when writing SQL SELECT statements, and can give the db a hint on what indexes to use, but the db's query optimisation engine is under no obligation to use the hint! (It also depends on the db whether you can do this.) On the whole however, indexes are a "set and forget" type performance optimisation done on the db.

Keep in mind that indexing a character or string based column is also not generally a good idea, as the indexes are very slow to build and use compared to a more discrete type, such as an Int.

AFAIK, indexes are not used through nHibernate. In each mapping file you do need to have a <id> element, which matches either the primary key or another candidate key. This is done as :

Code:
<id name="ReactionId" column="Reaction_Id" type="Int32" length="4" unsaved-value="0">
  <generator class="identity" />
</id>


and of course you can modify the type if your key is a string. But, I would strongly recommend using a surrogate Int type key.

Phil


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:49 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
I agree with you but my problem is that I need to search a record (in this case, a medical patient) by the first letter of Lastname. My code is:

Code:
internal static System.Collections.IList PatientsEnabled(NHibernate.ISession session, String searchExpression)
{
    IList result = null;
    try
    {
        result = session.CreateCriteria(typeof(PatientsEntity))
                        .Add(new GtExpression("Enabled", (System.Int16)0))
                        .Add(new LikeExpression("Last_name", searchExpression, MatchMode.Start))
                        .List();
    }
    finally
    {
        session.Close();
    }
    return result;
}


and at one other place:

result = session.CreateQuery("select count(*) from PatientsEntity where enabled > 0 and Last_name like :last_name")
                                        .SetParameter("last_name", (String)expression + "%", NHibernate.NHibernateUtil.String)
                                        .UniqueResult<System.Int64>();



If I create an index on the Last_Name column, the query is very faster.
And the patients table can have 500 000 records or plus ...

So, I have no choice. :-(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 4:05 am 
Beginner
Beginner

Joined: Fri Feb 29, 2008 9:36 am
Posts: 40
I'll finally create the index with

CREATE [UNIQUE] INDEX

It will be more simple.

Thanks for your help ! :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 2:25 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
If you are generating your schema from your mappings with hbm2ddl, you can specify indexes in your mapping using the 'index' attribute. There are other options for controlling ddl generation as well. See http://www.hibernate.org/hib_docs/nhibe ... etguide-s1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.