.

Thursday, April 4, 2019

Advantages And Disadvantages To Using Indexes Computer Science Essay

Advantages And Disadvantages To employ Indexes Computer Science EssayPut simply, database officees help speed up retrieval of data. The other salient benefit of top executivees is that your server doesnt have to work as hard to get the data. They atomic number 18 much the uniform as book advocatores, providing the database with quick jump points on where to find the full lengthiness (or to find the database row). on that point atomic number 18 both advantages and disadvantages to using indexes,however.One disadvantage is they erect support up sort of a chip of space harbor a textual matterbook or reference guide and youll see it takes quite a few pages to include those page references.Another disadvantage is using too mevery indexes clear actuall(a)y dimmed your database down. Thinking of a book again, imagine if e truly the, and or at was include in the index. That would stop the index being useful the index becomes as big as the text On top of that, each time a p age or database row is updated or removed, the reference or index also has to be updated.So indexes speed up finding data, but slow down inserting, updating or deleting data.Some expanses are automatically indexed. A primary separate or a celestial sphere marked as unique for congressman an email address, a userid or a social security number are automatically indexed so the database can quickly obtain to make sure that youre not going to introduce bad data.So when should a database field be indexed?The general rule is anything that is use to limit the number of results youre trying to find.Its hard to generalise so wholesome visualise at some specific but common examples.Note the database submits shown below are utilize as an example only and allow not necessarily be the best setup for your specific postulates.In a database delay that presents like thisNote The SQL code shown below works with both MySQL and PostgreSQL databases. nominate TABLE subscribers (subscribe rid INT firsthand KEY,emailaddress VARCHAR(255),firstname VARCHAR(255),lastname VARCHAR(255))if we want to quickly find an email address, we create an index on the emailaddress fieldCREATE INDEX subscriber_email ON subscribers(emailaddress) and any time we want to find an email address aim firstname, lastname FROM subscribers WHERE emailaddress=emailprotected it will be quite quick to findAnother reason for creating indexes is for tables that reference other tables. For example, in a CMS you might have a news program table that looks something like thisCREATE TABLE newsitem (newsid INT PRIMARY KEY,newstitle VARCHAR(255),newscontent TEXT,authorid INT,newsdate TIMESTAMP)and another table for authorsCREATE TABLE authors (authorid INT PRIMARY KEY,username VARCHAR(255),firstname VARCHAR(255),lastname VARCHAR(255))A head like thisSELECT newstitle, firstname, lastname FROM newsitem n, authors a WHERE n.authorid=a.authorid will be take advantage of an index on the newsitem authoridCREAT E INDEX newsitem_authorid ON newsitem(authorid)This allows the database to very quickly advert the records from the newsitem table to the authors table. In database terminology this is called a table join you should index any fields involved in a table join like this.Since the authorid in the authors table is a primary key, it is already indexed. The same goes for the newsid in the news table, so we dont need to look at those cases.On a side note, table aliases make things a rush easier to see whats happening. Using newsitem n and authors a means we dont have to writeSELECT newstitle, firstname, lastname FROM newsitem, authors WHERE newsitem.authorid=authors.authoridfor more entangled queries where more tables are referenced this can be extremely helpful and make things really easy to follow.In a more complicated example, a news item could exist in multiple categories, so in a origination like thisCREATE TABLE newsitem (newsid INT PRIMARY KEY,newstitle VARCHAR(255),newscontent TEXT,authorid INT,newsdate TIMESTAMP)CREATE TABLE newsitem_categories (newsid INT,categoryid INT)CREATE TABLE categories (categoryid INT PRIMARY KEY,categoryname VARCHAR(255))This interrogatorySELECT n.newstitle, c.categoryname FROM categories c, newsitem_categories nc, newsitem n WHERE c.categoryid=nc.categoryid AND nc.newsid=n.newsid will show all category names and newstitles for each category.To make this crabbed query fast we need to assay we have an index onnewsitem newsidnewsitem_categories newsidnewsitem_categories categoryidcategories categoryidNote Because the newsitem newsid and the categories categoryid fields are primary keys, they already have indexes.We need to watch there are indexes on the join table newsitem_categoriesThis will do itCREATE INDEX newscat_news ON newsitem_categories(newsid)CREATE INDEX newscat_cats ON newsitem_categories(categoryid)We could create an index like thisCREATE INDEX news_cats ON newsitem_categories(newsid, categoryid)However, doing t his limits some ways the index can be used. A query against the table that uses both newsid and categoryid will be able to use this index. A query against the table that only gets the newsid will be able to use the index.A query against that table that only gets the categoryid will not be able to use the index.For a table like thisCREATE TABLE example (a int,b int,c int)With this indexCREATE INDEX example_index ON example(a,b,c)It will be used when you check against a.It will be used when you check against a and b.It will be used when you check against a, b and c.It will not be used if you check against b and c, or if you only check b or you only check c.It will be used when you check against a and c but only for the a column it wont be used to check the c column as well.A query against a OR b like thisSELECT a,b,c FROM example where a=1 OR b=2Will only be able to use the index to check the a column as well it wont be able to use it to check the b column.Multi-column indexes have quite specific uses, so check their use carefully.Now that weve seen when we should use indexes, lets look at when we shouldnt use them. They can actually slow down your database (some databases may actually choose to issue the index if theres no reason to use it).A table like thisCREATE TABLE news (newsid INT PRIMARY KEY,newstitle VARCHAR(255),newscontent TEXT, alive(p) CHAR(1), ownd CHAR(1),newsdate TIMESTAMP) looks pretty standard. The active field tells us whether the news item is active and ready to be viewed on the site.So should we should create an index on this field for a query like this?SELECT newsid, newstitle FROM news WHERE active=1No, we shouldnt.If most of your content is live, this index will take up extra space and slow the query down because almost all of the fields match this criteria. Imagine 500 news items in the database with 495 being active. Its quicker to eliminate the ones that arent active than it is to list all of the active ones (if you do have an inde x on the active field, some databases will choose to bring down it anyway because it will slow the query down).The featured field tells us whether the news item should feature on the front page. Should we index this field? Yes. Most of our content is not featured, so an index on the featured column will be quite useful.Other examples of when to index a field include if youre going to order by it in a query. To get the most recent news items, we do a query like thisSELECT newtitle, newscontent FROM news ORDER BY newsdate DESCCreating an index on newsdate will allow the database to quickly sort the results so it can fetch the items in the right order. index can be a bit tricky to get right, however there are tools unattached for each database to help you work out if its working as it should.Well there you have it my trigger to database indexes. Hopefully youve learned something from this article and can apply what youve learned to your own databases.This entry was posted in Progra mming. Bookmark the permalink.22 Responses to Introduction to informationbase IndexesJim saysFebruary 17, 2006 at 713 amI think you need to be a bit more the reader knows absolutly nothing when describing the table joins. You lost me for a bit there. Perhaps a better step by step hand holding example would be better. Editors note Sure thing. upset see what I can come up with for next month If youre desperate for information and cant wait drop me a line chris at interspire dot com and Ill explain it further respondkhani saysMay 14, 2006 at 355 pmGood driveway chris,You ve described Indexes in a simple way. rejoinderVRS saysMay 24, 2006 at 132 pmGood article.Do include some explanation on clustered and non clustered indexes. reparteeVivek saysJuly 13, 2006 at 325 amGood article. Helped a lot in understading the basics of indexing. Thanks suffice apart(p) saysOctober 11, 2006 at 843 pmGood article man. I really appretiate your effort. stateAyaz saysNovember 14, 2006 at 922 amGoo d article to understand indexes for a beginner.solventDebiz saysNovember 27, 2006 at 521 pm precise well written and simply explained for those looking for a basic everywhereviewReplyNand saysDecember 14, 2006 at 1146 amGood article, felt like walking over the bridge on a gorge. Can u pl. explain drawbacks of using index also. Chris note The main drawback is that every insert, update or delete has to change the index as well. If you have a lot of indexes, that adds a lot of overhead to the operation. ReplyMyo saysDecember 19, 2006 at 1156 pmVery easy to understand and gives examples with differentsituations to demonstrate when and where we should use indexes and why.Thanks manReplyJohn Lowe says frame 14, 2007 at 257 amA quick a useful reminder to what idexes are all about, thanks.ReplyShravanti saysJune 26, 2007 at 311 amGood Introduction to Indexes. It would also be valuable to have information on how do indexes work on OLAP side of a Data Warehouse.ReplyHarsha saysAugust 13, 2 007 at 1121 pmcrisp tutorial.. good workReplykrish says family line 24, 2007 at 244 amReally very nice explanationReplyAlagesan saysOctober 10, 2007 at 1133 pmThis is a great article to learn indexing for beginners I really appreciate your efforts and good will in explaining them in words here.ThanksReplyHeather saysOctober 12, 2007 at 823 amThis was a great explanation of indexes for me I am self-taught when it comes to databases so the row in this tutorial was very easy for me to understand. Also, you used great examples to help explain your information. THANKSReplyJess Duckin saysOctober 28, 2007 at 458 amThe explaination on the usage of indexing is very helpfulReplyMayur saysOctober 29, 2007 at 156 pmThank you very much, a really informative tutorialfor me it was a 100% match to what I was looking for. ThanksReplysatish soni saysJanuary 11, 2008 at 717 amGreat article on indexes even oracle has not provided that much knowledge about indexesReplyShweta saysJanuary 11, 2008 at 4 25 pmGood. Just the overview i needed.ReplyHemant Jirange saysJanuary 17, 2008 at 339 amGreat articlethis is very simple to understand whole disadvantages about indexReplyramesh saysJanuary 18, 2008 at 226 amimpossible.even wikipedi couldnt match your tutorial on this topicthank uuuuuuuuuuuuuuu very muchReplyRavi saysSeptember 12, 2008 at 557 amthanks Chris, was an easy read for a database novice. I look forward to seeing the next chapterReplyLeave a ReplyName (required)Mail (will not be published) (required)WebsiteHome Email Marketing Shopping Cart Knowledge Management Software content Management Software Ecommerce Software Sell Products Online Our Guarantee Privacy Policy Copyright 1999-2010 Interspire Pty. Ltd. ACN 107 422 631

No comments:

Post a Comment