基本思路:
使用SQL SERVER 2014的Reporting Services
定位
MYSQL用于生产,MS SQL SERVER用于统计分析处理,不占用CPU和IO。
与MYSQL的关系
采用链接服务器方式: http://www.cnblogs.com/hukn/archive/2011/07/27/SQL_Server_ODBC_LinkServer_MySQL.html
创建Link Server
下面来创建一个与MySQL交互的链接(类似Oracle 的DBLink),在SQL Server 管理器中右键Server Objects 目录下的Linked Servers 点击“New Linked Server”:
Linked server 链接名称
Server Type 选择“Other data source”
Provider 选择“Microsoft OLE DB Provider for ODBC Drivers”
Product name 填写MySQL 主机地址
Provider String 填写 Driver={MySQL ODBC 5.1 Driver};Server=10.10.6.199;Database=dsideal_db; User=root;Password=dsideal;Option=3;
用例 :
SELECT * FROM OPENQUERY(MYSQL,'select * from dsideal_db.t_base_class')
INSERT INTO OPENQUERY(Servername,'select * from DBName.tablename') SELECT '1'
UPDATE OPENQUERY(Servername,'select * from DBName.tablename') SET colname='2' WHERE colname='1' SELECT * FROM OPENQUERY(Servername,'select * from DBName.tablename')
策略
依托于时间戳TS
USE [Demo] GO /****** Object: Table [dbo].[t_xktj_restj] Script Date: 2016/2/23 13:55:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[t_xktj_restj]( [ID] [INT] IDENTITY(1,1) NOT NULL, [XDKM] [VARCHAR](50) NOT NULL, [VERSION] [VARCHAR](50) NOT NULL, [APPTYPE] [VARCHAR](50) NOT NULL, [MEDIATYPE] [VARCHAR](50) NOT NULL, [RES_COUNT] [INT] NULL, [RES_SIZE] [INT] NULL, CONSTRAINT [PK_t_xktj_restj] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'学段学科' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'XDKM' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'版本' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'VERSION' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'应用类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'APPTYPE' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'媒体类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'MEDIATYPE' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'资源个数' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'RES_COUNT' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'资源体积' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N't_xktj_restj', @level2type=N'COLUMN',@level2name=N'RES_SIZE' GO
(1)全量: INIT
按每1000条,从MYSQL读取,然后写入到SQL SERVER。
直到写入完成。
(2)增量: 每两小时,从MYSQL通过AGENT定时获取,
-- 声明一个会话级临时表 IF object_id('tempdb..#itemtype_temporary') IS NOT NULL BEGIN DROP TABLE #itemtype_temporary END CREATE TABLE #itemtype_temporary ( Id int, resource_size_int int, resource_type INT, app_type_id int ) DECLARE @startTs NVARCHAR(2000) DECLARE @endTs NVARCHAR(2000) DECLARE @sql NVARCHAR(2000) SET @startTs='2016020816463800000' SET @endTs='2016022316463800000' SET @sql='INSERT INTO #itemtype_temporary SELECT id,resource_size_int,resource_type,app_type_id FROM OPENQUERY(MYSQL,''select * from dsideal_db.t_resource_info WHERE res_type=1 and update_ts between '+@startTs+' and '+@endTs+''')' -- PRINT @sql EXEC sp_executesql @sql SELECT * FROM #itemtype_temporary -- 然后利用游标进行统计表写入 declare @cursor cursor;--游标 declare @Id int DECLARE @resource_size_int int DECLARE @resource_type INT declare @app_type_id INT set @cursor=cursor for select id,resource_size_int,resource_type,app_type_id from #itemtype_temporary; open @cursor fetch next from @cursor into @id,@resource_size_int,@resource_type,@app_type_id; while @@FETCH_STATUS=0 begin --1、判断统计表中是不是存在此统计信息 --2、如果存在,则更新个数和体积 --3、如果不存在,则插入第一个数据 fetch next from @cursor into @id,@resource_size_int,@resource_type,@app_type_id; end close @cursor deallocate @cursor -- 删除临时表 DROP TABLE #itemtype_temporary
匿名访问的处理办法:
复杂的中国式报表如何实现?
示例学习:
创建基本表报表(SSRS 教程)
https://msdn.microsoft.com/zh-cn/library/ms167305%28v=sql.120%29.aspx
第 6 课:添加分组和总计 (Reporting Services)
https://msdn.microsoft.com/zh-CN/library/ms170712%28v=sql.120%29.aspx