Module | Sequel::Model::Associations::DatasetMethods |
In: |
lib/sequel/model/associations.rb
|
Eager loading makes it so that you can load all associated records for a set of objects in a single query, instead of a separate query for each object.
Two separate implementations are provided. eager should be used most of the time, as it loads associated records using one query per association. However, it does not allow you the ability to filter or order based on columns in associated tables. eager_graph loads all records in a single query using JOINs, allowing you to filter or order based on columns in associated tables. However, eager_graph can be slower than eager, especially if multiple one_to_many or many_to_many associations are joined.
You can cascade the eager loading (loading associations on associated objects) with no limit to the depth of the cascades. You do this by passing a hash to eager or eager_graph with the keys being associations of the current model and values being associations of the model associated with the current model via the key.
The arguments can be symbols or hashes with symbol keys (for cascaded eager loading). Examples:
Album.eager(:artist).all Album.eager_graph(:artist).all Album.eager(:artist, :genre).all Album.eager_graph(:artist, :genre).all Album.eager(:artist).eager(:genre).all Album.eager_graph(:artist).eager(:genre).all Artist.eager(:albums=>:tracks).all Artist.eager_graph(:albums=>:tracks).all Artist.eager(:albums=>{:tracks=>:genre}).all Artist.eager_graph(:albums=>{:tracks=>:genre}).all
You can also pass a callback as a hash value in order to customize the dataset being eager loaded at query time, analogous to the way the :eager_block association option allows you to customize it at association definition time. For example, if you wanted artists with their albums since 1990:
Artist.eager(:albums => proc{|ds| ds.filter{year > 1990}})
Or if you needed albums and their artist‘s name only, using a single query:
Albums.eager_graph(:artist => proc{|ds| ds.select(:name)})
To cascade eager loading while using a callback, you substitute the cascaded associations with a single entry hash that has the proc callback as the key and the cascaded associations as the value. This will load artists with their albums since 1990, and also the tracks on those albums and the genre for those tracks:
Artist.eager(:albums => {proc{|ds| ds.filter{year > 1990}}=>{:tracks => :genre}})
Add the eager! and eager_graph! mutation methods to the dataset.
# File lib/sequel/model/associations.rb, line 1331 1331: def self.extended(obj) 1332: obj.def_mutation_method(:eager, :eager_graph) 1333: end
If the expression is in the form x = y where y is a Sequel::Model instance, assume x is an association symbol and look up the association reflection via the dataset‘s model. From there, return the appropriate SQL based on the type of association and the values of the foreign/primary keys of y. For most association types, this is a simple transformation, but for many_to_many associations this creates a subquery to the join table.
# File lib/sequel/model/associations.rb, line 1419 1419: def complex_expression_sql(op, args) 1420: r = args.at(1) 1421: if (((op == '=''=' || op == '!=''!=') and r.is_a?(Sequel::Model)) || 1422: (multiple = ((op == :IN || op == 'NOT IN''NOT IN') and ((is_ds = r.is_a?(Sequel::Dataset)) or r.all?{|x| x.is_a?(Sequel::Model)})))) 1423: l = args.at(0) 1424: if ar = model.association_reflections[l] 1425: if multiple 1426: klass = ar.associated_class 1427: if is_ds 1428: if r.respond_to?(:model) 1429: unless r.model <= klass 1430: # A dataset for a different model class, could be a valid regular query 1431: return super 1432: end 1433: else 1434: # Not a model dataset, could be a valid regular query 1435: return super 1436: end 1437: else 1438: unless r.all?{|x| x.is_a?(klass)} 1439: raise Sequel::Error, "invalid association class for one object for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{klass.inspect}" 1440: end 1441: end 1442: elsif !r.is_a?(ar.associated_class) 1443: raise Sequel::Error, "invalid association class #{r.class.inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{ar.associated_class.inspect}" 1444: end 1445: 1446: if exp = association_filter_expression(op, ar, r) 1447: literal(exp) 1448: else 1449: raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}" 1450: end 1451: elsif multiple && (is_ds || r.empty?) 1452: # Not a query designed for this support, could be a valid regular query 1453: super 1454: else 1455: raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}" 1456: end 1457: else 1458: super 1459: end 1460: end
The preferred eager loading method. Loads all associated records using one query for each association.
The basic idea for how it works is that the dataset is first loaded normally. Then it goes through all associations that have been specified via eager. It loads each of those associations separately, then associates them back to the original dataset via primary/foreign keys. Due to the necessity of all objects being present, you need to use all to use eager loading, as it can‘t work with each.
This implementation avoids the complexity of extracting an object graph out of a single dataset, by building the object graph out of multiple datasets, one for each association. By using a separate dataset for each association, it avoids problems such as aliasing conflicts and creating cartesian product result sets if multiple one_to_many or many_to_many eager associations are requested.
One limitation of using this method is that you cannot filter the dataset based on values of columns in an associated table, since the associations are loaded in separate queries. To do that you need to load all associations in the same query, and extract an object graph from the results of that query. If you need to filter based on columns in associated tables, look at eager_graph or join the tables you need to filter on manually.
Each association‘s order, if defined, is respected. Eager also works on a limited dataset, but does not use any :limit options for associations. If the association uses a block or has an :eager_block argument, it is used.
# File lib/sequel/model/associations.rb, line 1361 1361: def eager(*associations) 1362: opt = @opts[:eager] 1363: opt = opt ? opt.dup : {} 1364: associations.flatten.each do |association| 1365: case association 1366: when Symbol 1367: check_association(model, association) 1368: opt[association] = nil 1369: when Hash 1370: association.keys.each{|assoc| check_association(model, assoc)} 1371: opt.merge!(association) 1372: else raise(Sequel::Error, 'Associations must be in the form of a symbol or hash') 1373: end 1374: end 1375: clone(:eager=>opt) 1376: end
The secondary eager loading method. Loads all associations in a single query. This method should only be used if you need to filter or order based on columns in associated tables.
This method builds an object graph using Dataset#graph. Then it uses the graph to build the associations, and finally replaces the graph with a simple array of model objects.
Be very careful when using this with multiple one_to_many or many_to_many associations, as you can create large cartesian products. If you must graph multiple one_to_many and many_to_many associations, make sure your filters are narrow if you have a large database.
Each association‘s order, if definied, is respected. eager_graph probably won‘t work correctly on a limited dataset, unless you are only graphing many_to_one and one_to_one associations.
Does not use the block defined for the association, since it does a single query for all objects. You can use the :graph_* association options to modify the SQL query.
Like eager, you need to call all on the dataset for the eager loading to work. If you just call each, you will get a normal graphed result back (a hash with table alias symbol keys and model object values).
# File lib/sequel/model/associations.rb, line 1399 1399: def eager_graph(*associations) 1400: ds = if @opts[:eager_graph] 1401: self 1402: else 1403: # Each of the following have a symbol key for the table alias, with the following values: 1404: # :reciprocals - the reciprocal instance variable to use for this association 1405: # :requirements - array of requirements for this association 1406: # :alias_association_type_map - the type of association for this association 1407: # :alias_association_name_map - the name of the association for this association 1408: clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :alias_association_type_map=>{}, :alias_association_name_map=>{}, :reciprocals=>{}, :cartesian_product_number=>0}) 1409: end 1410: ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations) 1411: end
Do not attempt to split the result set into associations, just return results as simple objects. This is useful if you want to use eager_graph as a shortcut to have all of the joins and aliasing set up, but want to do something else with the dataset.
# File lib/sequel/model/associations.rb, line 1466 1466: def ungraphed 1467: super.clone(:eager_graph=>nil) 1468: end
Call graph on the association with the correct arguments, update the eager_graph data structure, and recurse into eager_graph_associations if there are any passed in associations (which would be dependencies of the current association)
Arguments:
ds : | Current dataset |
model : | Current Model |
ta : | table_alias used for the parent association |
requirements : | an array, used as a stack for requirements |
r : | association reflection for the current association |
*associations : | any associations dependent on this one |
# File lib/sequel/model/associations.rb, line 1484 1484: def eager_graph_association(ds, model, ta, requirements, r, *associations) 1485: assoc_name = r[:name] 1486: assoc_table_alias = ds.unused_table_alias(assoc_name) 1487: loader = r[:eager_grapher] 1488: if !associations.empty? 1489: if associations.first.respond_to?(:call) 1490: callback = associations.first 1491: associations = {} 1492: elsif associations.length == 1 && (assocs = associations.first).is_a?(Hash) && assocs.length == 1 && (pr_assoc = assocs.to_a.first) && pr_assoc.first.respond_to?(:call) 1493: callback, assoc = pr_assoc 1494: associations = assoc.is_a?(Array) ? assoc : [assoc] 1495: end 1496: end 1497: ds = if loader.arity == 1 1498: loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>ta, :callback=>callback) 1499: else 1500: loader.call(ds, assoc_table_alias, ta) 1501: end 1502: ds = ds.order_more(*qualified_expression(r[:order], assoc_table_alias)) if r[:order] and r[:order_eager_graph] 1503: eager_graph = ds.opts[:eager_graph] 1504: eager_graph[:requirements][assoc_table_alias] = requirements.dup 1505: eager_graph[:alias_association_name_map][assoc_table_alias] = assoc_name 1506: eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array? 1507: eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2 1508: ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty? 1509: ds 1510: end
Check the associations are valid for the given model. Call eager_graph_association on each association.
Arguments:
ds : | Current dataset |
model : | Current Model |
ta : | table_alias used for the parent association |
requirements : | an array, used as a stack for requirements |
*associations : | the associations to add to the graph |
# File lib/sequel/model/associations.rb, line 1521 1521: def eager_graph_associations(ds, model, ta, requirements, *associations) 1522: return ds if associations.empty? 1523: associations.flatten.each do |association| 1524: ds = case association 1525: when Symbol 1526: ds.eager_graph_association(ds, model, ta, requirements, check_association(model, association)) 1527: when Hash 1528: association.each do |assoc, assoc_assocs| 1529: ds = ds.eager_graph_association(ds, model, ta, requirements, check_association(model, assoc), assoc_assocs) 1530: end 1531: ds 1532: else raise(Sequel::Error, 'Associations must be in the form of a symbol or hash') 1533: end 1534: end 1535: ds 1536: end
Build associations out of the array of returned object graphs.
# File lib/sequel/model/associations.rb, line 1539 1539: def eager_graph_build_associations(record_graphs) 1540: eager_graph = @opts[:eager_graph] 1541: master = eager_graph[:master] 1542: requirements = eager_graph[:requirements] 1543: alias_map = eager_graph[:alias_association_name_map] 1544: type_map = eager_graph[:alias_association_type_map] 1545: reciprocal_map = eager_graph[:reciprocals] 1546: 1547: # Make dependency map hash out of requirements array for each association. 1548: # This builds a tree of dependencies that will be used for recursion 1549: # to ensure that all parts of the object graph are loaded into the 1550: # appropriate subordinate association. 1551: dependency_map = {} 1552: # Sort the associations by requirements length, so that 1553: # requirements are added to the dependency hash before their 1554: # dependencies. 1555: requirements.sort_by{|a| a[1].length}.each do |ta, deps| 1556: if deps.empty? 1557: dependency_map[ta] = {} 1558: else 1559: deps = deps.dup 1560: hash = dependency_map[deps.shift] 1561: deps.each do |dep| 1562: hash = hash[dep] 1563: end 1564: hash[ta] = {} 1565: end 1566: end 1567: 1568: # This mapping is used to make sure that duplicate entries in the 1569: # result set are mapped to a single record. For example, using a 1570: # single one_to_many association with 10 associated records, 1571: # the main object will appear in the object graph 10 times. 1572: # We map by primary key, if available, or by the object's entire values, 1573: # if not. The mapping must be per table, so create sub maps for each table 1574: # alias. 1575: records_map = {master=>{}} 1576: alias_map.keys.each{|ta| records_map[ta] = {}} 1577: 1578: # This will hold the final record set that we will be replacing the object graph with. 1579: records = [] 1580: record_graphs.each do |record_graph| 1581: primary_record = record_graph[master] 1582: key = primary_record.pk_or_nil || primary_record.values.sort_by{|x| x[0].to_s} 1583: if cached_pr = records_map[master][key] 1584: primary_record = cached_pr 1585: else 1586: records_map[master][key] = primary_record 1587: # Only add it to the list of records to return if it is a new record 1588: records.push(primary_record) 1589: end 1590: # Build all associations for the current object and it's dependencies 1591: eager_graph_build_associations_graph(dependency_map, alias_map, type_map, reciprocal_map, records_map, primary_record, record_graph) 1592: end 1593: 1594: # Remove duplicate records from all associations if this graph could possibly be a cartesian product 1595: eager_graph_make_associations_unique(records, dependency_map, alias_map, type_map) if eager_graph[:cartesian_product_number] > 1 1596: 1597: # Replace the array of object graphs with an array of model objects 1598: record_graphs.replace(records) 1599: end