From: Claudio Nanni Date: May 7 2012 9:49am Subject: =?GB2312?Q?Re=3A_=BB=D8=B8=B4=A3=BA_=BB=D8=B8=B4=A3=BA_Why_is_creating_indexes_fast?= =?GB2312?Q?er_after_inserting_massive_data_rows=3F?= List-Archive: http://lists.mysql.com/mysql/227340 Message-Id: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=f46d0401fb27e9e2bb04bf6f2e23 --f46d0401fb27e9e2bb04bf6f2e23 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable too nice not to share it! http://www.youtube.com/watch?v=3DINHF_5RIxTE 2012/5/7 Zhangzhigang > Thanks, i thought about this answer in the past, and i appreciate your > reply. > > > > ________________________________ > =B7=A2=BC=FE=C8=CB=A3=BA Alex Schaft > =CA=D5=BC=FE=C8=CB=A3=BA mysql@stripped > =B7=A2=CB=CD=C8=D5=C6=DA=A3=BA 2012=C4=EA5=D4=C27=C8=D5, =D0=C7=C6=DA=D2= =BB, =CF=C2=CE=E7 4:59 > =D6=F7=CC=E2: Re: =BB=D8=B8=B4=A3=BA Why is creating indexes faster after= inserting massive data > rows? > > On 2012/05/07 10:53, Zhangzhigang wrote: > > johan .... > >> Plain and simple: the indices get updated after every insert statement= , > > whereas if you only create the index *after* the inserts, the index get= s > created in a single operation, which is a lot more efficient.. > > > > > > Ok, Creating the index *after* the inserts, the index gets created in a > single operation. > > But the indexes has to be updating row by row after the data rows has > all been inserted. Does it work in this way? > > So i can not find the different overhead about two ways. > My simplified 2c. When inserting rows with active indexes one by one > (insert), mysql has to > > 1) lock the space for the data to be added, > 2) write the data, > 3) lock the index, > 4) write the index key(s), > 5) unlock the index, > 6)unlock the data > > This happens for each row > > When first doing all data without index, only 1, 2, and 6 happen. When yo= u > then create an index, it can lock the index, read all the data and write > all index keys in one go and then unlock the index. > > If you make an omelet, do you fetch your eggs from the fridge one by one, > or all at the same time? :) > > HTH, > Alex > > > -- MySQL General Mailing List > For list archives: http://lists.mysql.com/mysql > To unsubscribe: http://lists.mysql.com/mysql > --=20 Claudio --f46d0401fb27e9e2bb04bf6f2e23--