2

SQL Server Management Studio で MDX クエリを実行すると、そのクエリの結果が 2 秒で得られます。6列で約400行を返します。

ADOMD を介して同じクエリを実行し、セルセットをループすると、約 5 分かかります。

ADOMD を使用してデータを取得する最速の方法と、このアプローチに時間がかかる理由を教えてください。

次のコードを使用しています。

namespace Delete
{
    public class TreeNode
    {
        public string MemberName { get; set; }
        public string ID { get; set; }
        public string ParentKey { get; set; }
        public int Level { get; set; }
        public string HierarchyLevel { get; set; }
        public bool root { get; set; }
        public bool leaf { get; set; }

        public bool expanded
        {
            get { return true; }
        }
        public bool @checked
        {
            get { return false; }
        }
        public List<TreeNode> children { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var v = ExecuteQueryForHierarchy("", "");
        }


        private static List<TreeNode> ExecuteQueryForHierarchy(string connString, string query)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();


            CellSet cellset;
            connString = "";
            query = @"";
            List<TreeNode> treeNodeList = new List<TreeNode>();
            var connection = new AdomdConnection(connString);
            var command = new AdomdCommand(query, connection);
            try
            {
                connection.Open();
                cellset = command.ExecuteCellSet();

                TreeNode node = null;
                var positionCollection = cellset.Axes[1].Positions;


                foreach (var item in positionCollection)
                {
                    node = new TreeNode();
                    node.MemberName = item.Members[0].Caption;
                    node.Level = item.Members[0].LevelDepth;
                    node.HierarchyLevel = item.Members[0].LevelName;
                    node.ParentKey = item.Members[0].Parent != null ? item.Members[0].Parent.UniqueName : null;
                    node.root = item.Members[0].Parent == null ? true : false;
                    node.leaf = item.Members[0].ChildCount <= 0;
                    node.ID = item.Members[0].UniqueName;
                    treeNodeList.Add(node);
                    Console.WriteLine(treeNodeList.Count);
                }
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }

            sw.Stop();
            TimeSpan elapsedTime = sw.Elapsed;
            Console.WriteLine(sw.Elapsed.ToString());
            Console.ReadKey();
            return treeNodeList;
        }

        private List<TreeNode> BuildTree(IEnumerable<TreeNode> items)
        {
            List<TreeNode> itemL = items.ToList();
            itemL.ForEach(i => i.children = items.Where(ch => ch.ParentKey == i.ID).ToList());
            return itemL.Where(i => i.ParentKey == null).ToList();
        }

    }
}
4

1 に答える 1