什么是 SQL Server 2016 R Services?What is SQL Server 2016 R Services?

仅限 适用于:Applies to: 是SQL Server 2016 (13.x)SQL Server 2016 (13.x)yesSQL Server 2016 (13.x)SQL Server 2016 (13.x)适用于:Applies to: 是SQL Server 2016 (13.x)SQL Server 2016 (13.x)yesSQL Server 2016 (13.x)SQL Server 2016 (13.x) only

R Services 是 SQL Server 2016 中的一项功能,借助此功能可以使用关系数据运行 R 脚本。R Services is a feature in SQL Server 2016 that gives the ability to run R scripts with relational data. 可以使用开源包和框架,以及 Microsoft R 包进行预测分析和机器学习。You can use open-source packages and frameworks, and the Microsoft R packages for predictive analytics and machine learning. 脚本在数据库中执行,而不将数据移动到 SQL Server 外部或是在网络上移动。The scripts are executed in-database without moving data outside SQL Server or over the network. 本文介绍 SQL Server R Services 的基础知识。This article explains the basics of SQL Server R Services.


在 SQL Server 2017 及更高版本中,R Services 被重命名为机器学习服务,并支持 Python 和 R。R Services was renamed to Machine Learning Services in SQL Server 2017 and later, and supports both Python and R.

什么是 R Services?What is R Services?

使用 SQL Server R Services,可以在数据库中执行 R 脚本。SQL Server R Services lets you execute R scripts in-database. 可以使用它来准备和清理数据、执行特征工程以及在数据库中定型、评估和部署机器学习模型。You can use it to prepare and clean data, do feature engineering, and train, evaluate, and deploy machine learning models within a database. 此功能在数据所在的位置运行脚本,无需通过网络将数据传输到其他服务器。The feature runs your scripts where the data resides and eliminates transfer of the data across the network to another server.

R Services 中包含 R 的基础分发。Base distributions of R are included in R Services. 除了 Microsoft 包RevoScaleRMicrosoftML、[olapR]../r/ref-r-olapr.md) 和 sqlrutils 之外,还可以使用开源包和框架。You can use open-source packages and frameworks in addition to the Microsoft packages RevoScaleR, MicrosoftML, [olapR]../r/ref-r-olapr.md), and sqlrutils for R.

R Services 使用扩展性框架在 SQL Server 中运行 R 脚本。R Services uses an extensibility framework to run R scripts in SQL Server. 详细了解工作原理:Learn more about how this works:

使用 R Services 可以执行哪些操作?What can I do with R Services?

可以使用 R Services 在 SQL Server 中生成和定型机器学习和深度学习模型。You can use R Services to build and training machine learning and deep learning models within SQL Server. 还可以将现有模型部署到 R Services,并使用关系数据进行预测。You can also deploy existing models to R Services and use relational data for predictions.

可以使用 SQL Server R Services 进行多种类型的预测,下面是一些示例:Examples of the type of predictions that you can use SQL Server R Services for, include:

预测类型Prediction type 示例Example
分类/类别Classification/Categorization 自动将客户反馈分为积极和消极两类Automatically divide customer feedback into positive and negative categories
回归/预测连续值Regression/Predict continuous values 根据面积和地段预测房价Predict the price of houses based on size and location
异常检测Anomaly Detection 检测欺诈性的银行交易Detect fraudulent banking transactions
建议Recommendations 根据网购者以前的购买情况,推荐他们可能想购买的产品Suggest products that online shoppers may want to buy, based on their previous purchases

如何执行 R 脚本How to execute R scripts

有两种方法可以在 R Services 中执行 R 脚本:There are two ways to execute R scripts in R Services:

R 版本R versions

下面列出了 SQL Server 2016 R 服务中包含的 R 运行时版本。The following lists the versions of the R runtime that are included in SQL Server 2016 R Services.

SQL Server 版本SQL Server version 默认 R 运行时版本Default R runtime versions
SQL Server 2016 RTM - SP2 CU13SQL Server 2016 RTM - SP2 CU13
SQL Server 2016 SP2 CU14 及更高版本SQL Server 2016 SP2 CU14 and later 3.2.2 和 and 3.5.2

SQL Server 2016 Service Pack (SP) 2 及更高版本的累积更新 (CU) 14 包括更新的 R 运行时。Cumulative Update (CU) 14 for SQL Server 2016 Service Pack (SP) 2 and later include newer R runtimes. 有关详细信息,请参阅更改默认语言运行时版本For more information, see Change the default language runtime version.

对于 R 的其他版本或者如果要运行 Python,请使用用于 SQL Server 2017 及更高版本的机器学习服务For other versions of R, or to run Python, use Machine Learning Services for SQL Server 2017 and later.

R 包R packages

除了 Microsoft 的企业包外,还可以使用开源包和框架。You can use open-source packages and frameworks, in addition to Microsoft's enterprise packages. 大多数常见的开源 R 包已预先安装在 R Services 中。Most common open-source R packages are pre-installed in R Services. 还包括 Microsoft 提供的以下 R 包:The following R packages from Microsoft are also included:

程序包Package 说明Description
RevoScaleRRevoScaleR 用于可缩放 R 的主包。数据转换和操作、统计摘要、可视化和多种形式的建模。The primary package for scalable R. Data transformations and manipulation, statistical summarization, visualization, and many forms of modeling. 此外,此包中的函数会自动在可用内核之间分配工作负载以进行并行处理。Additionally, functions in this package automatically distribute workloads across available cores for parallel processing.
MicrosoftML (R)MicrosoftML (R) 添加机器学习算法,以便为文本分析、图像分析和情绪分析创建自定义模型。Adds machine learning algorithms to create custom models for text analysis, image analysis, and sentiment analysis.
olapRolapR R 函数,用于针对 SQL Server Analysis Services OLAP 多维数据集进行 MDX 查询。R functions used for MDX queries against a SQL Server Analysis Services OLAP cube.
sqlrutilssqlrutils 一种在 T-SQL 存储过程中使用 R 脚本、将该存储过程注册到数据库并在 R 开发环境中运行存储过程的机制。A mechanism to use R scripts in a T-SQL stored procedure, register that stored procedure with a database, and run the stored procedure from an R development environment.
Microsoft R OpenMicrosoft R Open Microsoft R Open (MRO) 是 Microsoft 中 R 的增强型分发。Microsoft R Open (MRO) is the enhanced distribution of R from Microsoft. 它是用于统计分析和数据科学的完整开源平台。It is a complete open-source platform for statistical analysis and data science. 它基于 R,并 100% 与 R 兼容,包括很多其他功能,可以带来更高的性能和可再现性。It is based on and 100% compatible with R, and includes additional capabilities for improved performance and reproducibility.

如何开始使用 RServices?How do I get started with RServices?

  1. 安装 SQL Server 2016 R ServicesInstall SQL Server 2016 R Services

  2. 配置开发工具。Configure your development tools. 可用工具如下:You can use:

  3. 编写第一个 R 脚本Write your first R script

后续步骤Next steps