📄 join.so
字号:
m4_comment([$Id: join.so,v 10.31 2004/01/21 20:39:56 bostic Exp $])m4_ref_title(Access Methods, Equality Join, [@equality join, equality @join, @natural join], am/curdup, am/count)m4_p([dnlm4_db supports "equality" (also known as "natural"), joins on secondaryindices. An equality join is a method of retrieving data from a primarydatabase using criteria stored in a set of secondary indices. Itrequires the data be organized as a primary database which contains theprimary key and primary data field, and a set of secondary indices.Each of the secondary indices is indexed by a different secondary key,and, for each key in a secondary index, there is a set of duplicate dataitems that match the primary keys in the primary database.])m4_p([dnlFor example, let's assume the need for an application that will returnthe names of stores in which one can buy fruit of a given color. Wewould first construct a primary database that lists types of fruit asthe key item, and the store where you can buy them as the data item:])m4_indentv([dnlm4_table_beginm4_table_header(Primary key:, Primary data:)m4_table_element(_left, apple, Convenience Store)m4_table_element(_left, blueberry, Farmer's Market)m4_table_element(_left, peach, Shopway)m4_table_element(_left, pear, Farmer's Market)m4_table_element(_left, raspberry, Shopway)m4_table_element(_left, strawberry, Farmer's Market)m4_table_end])m4_p([dnlWe would then create a secondary index with the key m4_bold(color), and,as the data items, the names of fruits of different colors.])m4_indentv([dnlm4_table_beginm4_table_header(Secondary key:, Secondary data:)m4_table_element(_left, blue, blueberry)m4_table_element(_left, red, apple)m4_table_element(_left, red, raspberry)m4_table_element(_left, red, strawberry)m4_table_element(_left, yellow, peach)m4_table_element(_left, yellow, pear)m4_table_end])m4_p([dnlThis secondary index would allow an application to look up a color, andthen use the data items to look up the stores where the colored fruitcould be purchased. For example, by first looking up m4_bold(blue),the data item m4_bold(blueberry) could be used as the lookup key in theprimary database, returning m4_bold(Farmer's Market).])m4_p([dnlYour data must be organized in the following manner in order to use them4_refT(dbh_join):])m4_nlistbeginm4_nlist([dnlThe actual data should be stored in the database represented by them4_ref(Db) object used to invoke this method. Generally, thism4_ref(Db) object is called the m4_italic(primary).])m4_nlist([dnlSecondary indices should be stored in separate databases, whose keysare the values of the secondary indices and whose data items are theprimary keys corresponding to the records having the designatedsecondary key value. It is acceptable (and expected) that there may beduplicate entries in the secondary indices.m4_p([dnlThese duplicate entries should be sorted for performance reasons, althoughit is not required. For more information see the m4_ref(DB_DUPSORT) flagto the m4_refT(dbh_set_flags).])])m4_nlistendm4_p([dnlWhat the m4_refT(dbh_join) does is review a list of secondary keys, and,when it finds a data item that appears as a data item for all of thesecondary keys, it uses that data item as a lookup into the primarydatabase, and returns the associated data item.])m4_p([dnlIf there were another secondary index that had as its key the m4_bold(cost)of the fruit, a similar lookup could be done on stores where inexpensivefruit could be purchased:])m4_indentv([dnlm4_table_beginm4_table_header(Secondary key:, Secondary data:)m4_table_element(_left, expensive, blueberry)m4_table_element(_left, expensive, peach)m4_table_element(_left, expensive, pear)m4_table_element(_left, expensive, strawberry)m4_table_element(_left, inexpensive, apple)m4_table_element(_left, inexpensive, pear)m4_table_element(_left, inexpensive, raspberry)m4_table_end])m4_p([dnlThe m4_refT(dbh_join) provides equality join functionality. While notstrictly cursor functionality, in that it is not a method off a cursorhandle, it is more closely related to the cursor operations than to thestandard m4_ref(Db) operations.])m4_p([dnlIt is also possible to do lookups based on multiple criteria in a singleoperation. For example, it is possible to look up fruits that are bothred and expensive in a single operation. If the same fruit appeared asa data item in both the color and expense indices, then that fruit namewould be used as the key for retrieval from the primary index, and wouldthen return the store where expensive, red fruit could be purchased.])m4_section(Example)m4_p([dnlConsider the following three databases:])m4_tagbeginm4_tag(personnel, [dnlm4_bulletbeginm4_bullet([key = SSN])m4_bullet([data = record containing name, address, phone number, job title])m4_bulletend])m4_tag(lastname, [dnlm4_bulletbeginm4_bullet([key = lastname])m4_bullet([data = SSN])m4_bulletend])m4_tag(jobs, [dnlm4_bulletbeginm4_bullet([key = job title])m4_bullet([data = SSN])m4_bulletend])m4_tagendm4_p([dnlConsider the following query:])m4_indent([dnlReturn the personnel records of all people named smith with the jobtitle manager.])m4_p([dnlThis query finds are all the records in the primary database (personnel)for whom the criteria m4_bold([lastname=smith and job title=manager]) istrue.])m4_p([dnlAssume that all databases have been properly opened and have thehandles: pers_db, name_db, job_db. We also assume that we have anactive transaction to which the handle txn refers.])include(ref/am/join.cs)m4_p([dnlThe name cursor is positioned at the beginning of the duplicate listfor m4_bold([smith]) and the job cursor is placed at the beginning ofthe duplicate list for m4_bold([manager]). The join cursor is returnedfrom the join method. This code then loops over the join cursor gettingthe personnel records of each one until there are no more.])m4_page_footer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -