]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1928 Improve performances by splitting the SQL query in 2
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 26 Oct 2011 13:07:45 +0000 (15:07 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 26 Oct 2011 13:10:49 +0000 (15:10 +0200)
plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_metric.html.erb
plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/hotspot_most_violated_resources.html.erb

index 894d54a5c8b47d2d16856f4ed7db33cb24f0b6b7..94769ac1b4ed06f7f51e9545bc9d79eead2fc631 100644 (file)
   end
   
   snapshots = nil
-  if metric.numeric?
-    snapshots_conditions = ["snapshots.scope = 'FIL'", "project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"]
-    snapshots_values = {}
-    snapshots_conditions << "snapshots.path LIKE :path"
-    snapshots_values[:path] = "#{@snapshot.path}#{@snapshot.id}.%"
-    snapshots_conditions << "project_measures.metric_id = :m_id"
-    snapshots_values[:m_id] = metric.id
-    snapshots_conditions << "snapshots.root_project_id = :root_id"
-    snapshots_values[:root_id] = @snapshot.root_project_id
-  
-    snapshots=Snapshot.find(:all, 
-                            :conditions => [snapshots_conditions.join(' AND '), snapshots_values], 
-                            :include => ['project', 'measures'],
-                            :order => "project_measures.value #{'DESC' if metric.direction<0}",
-                            :limit => limit)
+  if metric.numeric?    
+    snapshots_conditions=["snapshots.scope = 'FIL'", "snapshots.islast=:islast"]
+    snapshots_values={:islast => true}
+    snapshots_conditions << '(snapshots.id=:sid OR (snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path))'
+    snapshots_values[:sid]=@snapshot.id
+    snapshots_values[:root_sid] = (@snapshot.root_snapshot_id || @snapshot.id)
+    snapshots_values[:path]="#{@snapshot.path}#{@snapshot.id}.%"
+        
+    measures_conditions = ["project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"]
+    measures_values = {}
+    measures_conditions << "project_measures.metric_id = :m_id"
+    measures_values[:m_id] = metric.id
+    
+    measures=ProjectMeasure.find(:all,
+                                 :joins => :snapshot, 
+                                 :conditions => [ (snapshots_conditions + measures_conditions).join(' AND '), snapshots_values.merge(measures_values)],
+                                 :order => "project_measures.value #{'DESC' if metric.direction<0}",
+                                 :limit => limit)
+    
+    snapshots=Snapshot.find(measures.map {|m| m.snapshot_id}, :include => 'project')
+    snapshots_by_id = {}
+    snapshots.each do |s|
+      snapshots_by_id[s.id]=s
+    end
   end
 %>
 
@@ -53,9 +62,8 @@
   else
     metric_max_value = snapshots.first.measure(metric).value
   end
-  snapshots.each do |s|
-    measure = s.measure(metric)
-    resource = s.resource
+  measures.each do |measure|
+    resource = snapshots_by_id[measure.snapshot_id].resource
 %>
     <tr class="<%= cycle 'even','odd', :name => ('hotspot_metric' + widget.id.to_s) -%>">
       <td>
index 8685fc47ce889bd17315e89063b21de987567158..3f457c7dfbf8d06560ab59901554be996f1e7b96 100644 (file)
@@ -6,23 +6,30 @@
 
   metric = Metric.find(:first, :conditions => "name = 'weighted_violations'")
   
-  snapshots_conditions = ["snapshots.scope = 'FIL'"]
-  snapshots_values = {}
+  snapshots_conditions=["snapshots.scope = 'FIL'", "snapshots.islast=:islast"]
+  snapshots_values={:islast => true}
   snapshots_conditions << "(snapshots.qualifier = 'CLA' OR snapshots.qualifier = 'FIL' OR snapshots.qualifier = 'TRK')"
-  snapshots_conditions << "project_measures.rule_id IS NULL"
-  snapshots_conditions << "project_measures.characteristic_id IS NULL"
-  snapshots_conditions << "snapshots.path LIKE :path"
-  snapshots_values[:path] = "#{@snapshot.path}#{@snapshot.id}.%"
-  snapshots_conditions << "project_measures.metric_id = :m_id"
-  snapshots_values[:m_id] = metric.id
-  snapshots_conditions << "snapshots.root_project_id = :root_id"
-  snapshots_values[:root_id] = @snapshot.root_project_id
-
-  snapshots=Snapshot.find(:all, 
-                          :conditions => [snapshots_conditions.join(' AND '), snapshots_values], 
-                          :include => ['project', 'measures'],
-                          :order => "project_measures.value DESC",
-                          :limit => limit)
+  snapshots_conditions << '(snapshots.id=:sid OR (snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path))'
+  snapshots_values[:sid]=@snapshot.id
+  snapshots_values[:root_sid] = (@snapshot.root_snapshot_id || @snapshot.id)
+  snapshots_values[:path]="#{@snapshot.path}#{@snapshot.id}.%"
+      
+  measures_conditions = ["project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL"]
+  measures_values = {}
+  measures_conditions << "project_measures.metric_id = :m_id"
+  measures_values[:m_id] = metric.id
+  
+  measures=ProjectMeasure.find(:all,
+                               :joins => :snapshot, 
+                               :conditions => [ (snapshots_conditions + measures_conditions).join(' AND '), snapshots_values.merge(measures_values)],
+                               :order => "project_measures.value #{'DESC' if metric.direction<0}",
+                               :limit => limit)
+  
+  snapshots=Snapshot.find(measures.map {|m| m.snapshot_id}, :include => 'project')
+  snapshots_by_id = {}
+  snapshots.each do |s|
+    snapshots_by_id[s.id]=s
+  end  
 %>
 
 <div class="line-block">
   <thead><tr><th colspan="11"/></tr></thead>
   <tbody>
 <%
-  snapshots.each do |s|
-    resource = s.resource
+  measures.each do |measure|
+    resource = snapshots_by_id[measure.snapshot_id].resource
     violations_per_severity={}
-    s.measure(metric).text_value.split(';').each do |part|
+    measure.text_value.split(';').each do |part|
       fields=part.split('=')
       violations_per_severity[fields[0]]=fields[1]
     end