private void GenerateRange(IEnumerable <RangeHPartitioningAttributeDefinition> attributePartitions, List <RangeHPartitionAttributeDefinition> partitionParts,
                            RelationData relation, ref VirtualHPartitioningDefinition result)
 {
     if (attributePartitions.Count() > 0)
     {
         var first = attributePartitions.First();
         foreach (var p in first.Partitions)
         {
             var pp = new List <RangeHPartitionAttributeDefinition>(partitionParts);
             pp.Add(p);
             GenerateRange(attributePartitions.Skip(1), pp, relation, ref result);
         }
     }
     else
     {
         var toPart = partitionParts.Take(partitionParts.Count - 1).Select(x => toSqlValueStringConverter.ConvertStringRepresentation(x.DbType, x.FromValueInclusive)).ToList();
         toPart.Add(partitionParts.Select(x => toSqlValueStringConverter.ConvertStringRepresentation(x.DbType, x.ToValueExclusive)).Last());
         result.PartitionStatements.Add(String.Format("PARTITION OF {0}.{1} FOR VALUES FROM ({2}) TO ({3})",
                                                      relation.SchemaName, relation.Name,
                                                      String.Join(",", partitionParts.Select(x => toSqlValueStringConverter.ConvertStringRepresentation(x.DbType, x.FromValueInclusive))),
                                                      String.Join(",", toPart)
                                                      )
                                        );
     }
 }
Exemplo n.º 2
0
        protected override void OnExecute()
        {
            using (var scope = new DatabaseScope(context.Database.Name))
            {
                virtualIndicesRepository.DestroyAll();
                foreach (var index in context.IndicesDesignData.PossibleIndices.All)
                {
                    Dictionary <AttributeData, List <string> > possibleFilteredAttributeValues = new Dictionary <AttributeData, List <string> >();
                    foreach (var a in index.Attributes)
                    {
                        List <string> mostSignificantValues  = new List <string>();
                        List <string> leastSignificantValues = new List <string>();
                        if (a.MostCommonValuesFrequencies != null && a.MostCommonValuesFrequencies.Length >= 2)// we need at least two values
                        {
                            decimal frequenciesSum = 0;
                            for (int i = 0; i < Math.Min(a.MostCommonValuesFrequencies.Length - 1, MOST_COMMON_VALUES_MAX_COUNT); i++)
                            {
                                if (a.MostCommonValuesFrequencies[i] >= MOST_COMMON_VALUE_MIN_FREQUENCY)
                                {
                                    var sqlStringValue = toSqlValueStringConverter.ConvertStringRepresentation(a.DbType, a.MostCommonValues[i]);
                                    if (sqlStringValue != null)
                                    {
                                        mostSignificantValues.Add(sqlStringValue);
                                        frequenciesSum += a.MostCommonValuesFrequencies[i];
                                    }
                                }
                            }
                            if (frequenciesSum < MOST_COMMON_VALUES_MIN_FREQUENCIES_SUM || frequenciesSum > MOST_COMMON_VALUES_MAX_FREQUENCIES_SUM)
                            {
                                mostSignificantValues.Clear();
                            }

                            frequenciesSum = 0;
                            for (int i = a.MostCommonValuesFrequencies.Length - 1; i >= Math.Max(1, a.MostCommonValuesFrequencies.Length - MOST_COMMON_VALUES_MAX_COUNT + 1); i--)
                            {
                                var sqlStringValue = toSqlValueStringConverter.ConvertStringRepresentation(a.DbType, a.MostCommonValues[i]);
                                if (sqlStringValue != null)
                                {
                                    leastSignificantValues.Add(sqlStringValue);
                                    frequenciesSum += a.MostCommonValuesFrequencies[i];
                                }
                            }
                            if (frequenciesSum > MOST_COMMON_VALUES_MIN_FREQUENCIES_SUM)
                            {
                                leastSignificantValues.Clear();
                            }
                        }
                        if (mostSignificantValues.Count > 0)
                        {
                            possibleFilteredAttributeValues.Add(a, mostSignificantValues);
                        }
                        else if (leastSignificantValues.Count > 0)
                        {
                            possibleFilteredAttributeValues.Add(a, leastSignificantValues);
                        }
                    }
                    if (possibleFilteredAttributeValues.Count > 0)
                    {
                        string filter             = CreateFilterString(possibleFilteredAttributeValues);
                        var    targetRelationData = context.RelationsData.GetReplacementOrOriginal(index.Relation.ID);
                        var    virtualIndex       = virtualIndicesRepository.Create(dbObjectDefinitionGenerator.Generate(index.WithReplacedRelation(targetRelationData), filter));
                        var    size    = virtualIndicesRepository.GetVirtualIndexSize(virtualIndex.ID);
                        var    filters = new Dictionary <string, long>();
                        filters.Add(filter, size);
                        context.IndicesDesignData.PossibleIndexFilters.Add(index, filters);
                    }
                }
            }
        }