<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; 数据库</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e6%95%b0%e6%8d%ae%e5%ba%93/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>理工科背景的证券从业人员</description>
	<lastBuildDate>Sun, 05 Feb 2012 03:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Matlab中使用ADODB访问数据库</title>
		<link>http://zhiqiang.org/blog/it/use-adodb-in-matlab.html</link>
		<comments>http://zhiqiang.org/blog/it/use-adodb-in-matlab.html#comments</comments>
		<pubDate>Sat, 09 Oct 2010 06:54:40 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[ADODB]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[我贡献的源代码]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=1213</guid>
		<description><![CDATA[博客 » IT技术 » matlab » 系列：我贡献的源代码 查看该系列所有文章 Matlab的database工具箱只支持ODBC数据库连接，在使用之前需要先设置ODBC数据源。之前在Excel里用的是ADODB，使用ADODB的好处是无需设置ODBC源，似乎效率也要高一些（但我一直没弄懂ADODB是什么东西，所以效率这东西我也说不清）。 后来在matlab central上查了下，原来Matlab可以通过调用activex的方式使用ADODB。然后我...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/matlab'>matlab</a>  » </p><div class="series"><span>系列：<b>我贡献的源代码</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/zhiqiangcodes">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>Matlab的database工具箱只支持ODBC数据库连接，在使用之前需要先设置ODBC数据源。之前在Excel里用的是ADODB，使用ADODB的好处是无需设置ODBC源，似乎效率也要高一些（但我一直没弄懂ADODB是什么东西，所以效率这东西我也说不清）。</p>
<p>后来在matlab central上查了下，原来Matlab可以通过调用activex的方式使用ADODB。然后我照着别人的例子封装成了一个类。简单的测试发现这种方法比封装的database要快4倍。</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>使用这个类就很简单了，比如如果手头已有数据库的连接字符串，可以这么调用：</p>
<pre>ado = adodb;
res = ado.query('select * from sys', ...
    'Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;');</pre>
<p>或者直接提供数据库信息</p>
<pre>ado = adodb;
res = ado.query(sql, {database_type, server_name_or_ip, ...
    database_name, user, password});</pre>
<p>更详细使用方法可在下载后 help adodb。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/date-series-in-matlab-excel-sql.html">Matlab、Excel、SQL中的日期的数字序列形式</a></li><li><a href="http://zhiqiang.org/blog/it/pivottable-in-matlab.html">Matlab里的PivotTable</a></li><li><a href="http://zhiqiang.org/blog/finance/riskmanage/var-excel-vba-code.html">计算VaR的VBA代码和Excel模板</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html">最大回撤和最大短期回撤的线性算法</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a></li><li><a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">用脚本添加ODBC数据源</a></li><li><a href="http://zhiqiang.org/blog/it/config-64bit-matlab-compiler.html">配置64位Matlab的编译器</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2010. | <a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html#comments">8 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/use-adodb-in-matlab.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>用脚本添加ODBC数据源</title>
		<link>http://zhiqiang.org/blog/it/script-to-add-odbc-source.html</link>
		<comments>http://zhiqiang.org/blog/it/script-to-add-odbc-source.html#comments</comments>
		<pubDate>Tue, 05 Oct 2010 02:10:15 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[ODBC]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[编程和算法效率]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=1206</guid>
		<description><![CDATA[博客 » IT技术 » 编程和算法效率 » 系列：编程和算法效率 查看该系列所有文章 Excel、Matlab在与数据库交互数据时，通常需要事先配置好ODBC数据源，这导致一个文件换到另一台机器还需要重新配置ODBC。手工配置太麻烦，从国外一个网站看到一种解决方案： 运行regedit，展开到HKEY_CURRENT_USER\Software\ODBC\ODBC.INI，导出所有该目录下所有注册项，保存为.reg文件，然后换台机器运行该r...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/%e7%bc%96%e7%a8%8b%e5%92%8c%e7%ae%97%e6%b3%95%e6%95%88%e7%8e%87'>编程和算法效率</a>  » </p><div class="series"><span>系列：<b>编程和算法效率</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/%e7%bc%96%e7%a8%8b%e5%92%8c%e7%ae%97%e6%b3%95%e6%95%88%e7%8e%87">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>Excel、Matlab在与数据库交互数据时，通常需要事先配置好ODBC数据源，这导致一个文件换到另一台机器还需要重新配置ODBC。手工配置太麻烦，从国外一个网站看到一种解决方案：</p>
<p>运行regedit，展开到HKEY_CURRENT_USER\Software\ODBC\ODBC.INI，导出所有该目录下所有注册项，保存为.reg文件，然后换台机器运行该reg文件即可。</p>
<p>也可以自己直接编辑注册表脚本。比如下面定义了一个SQL的数据源：</p>
<pre>Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\ODBC\ODBC.INI]

[HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources]
"%sql_odbc_name%"="SQL Server"

[HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\%sql_odbc_name%]
"Driver"="C:\\WINDOWS\\system32\\SQLSRV32.dll"
"Description"="%sql_odbc_name%"
"Server"="%sql_server_address%"</pre>
<p>Oracle数据库的脚本定义稍微复杂点，但思路也差不多。模板见下面文件（需替换所有%%变量）</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>如需在Matlab里直接执行注册表脚本，只需在command line或者m文件里使用winopen('abc.reg')即可。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li><li><a href="http://zhiqiang.org/blog/it/for-in-matlab.html">Matlab的for循环</a></li><li><a href="http://zhiqiang.org/blog/it/call-by-value-or-reference-in-matlab.html">Matlab的传值调用的效率问题</a></li><li><a href="http://zhiqiang.org/blog/it/class-wrapper-functions-in-matlab.html">Matlab中用类封装函数</a></li><li><a href="http://zhiqiang.org/blog/it/save-load-data-v6-is-fast.html">尽量使用Matlab的save -v6</a></li><li><a href="http://zhiqiang.org/blog/it/undocumented-matlab-funtions-and-features.html">使用Matlab隐藏的函数和功能</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2010. | <a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html#comments">0&#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/script-to-add-odbc-source.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>动态修改Excel数据表的数据来源</title>
		<link>http://zhiqiang.org/blog/it/change-pivotcache-source.html</link>
		<comments>http://zhiqiang.org/blog/it/change-pivotcache-source.html#comments</comments>
		<pubDate>Wed, 13 Jan 2010 04:44:25 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[办公自动化]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[数据透视表]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=1056</guid>
		<description><![CDATA[博客 » IT技术 » Excel » 系列：办公自动化 查看该系列所有文章 Excel有一个很有用的功能是直接导入外部数据库或者使用外部数据源建立数据透视表和数据透视图。但比较可惜的是，这个数据源的查询语句是静态的，它无法根据日期自动修改（比如在应用中，我们希望每天获取的外部数据都是当天最新的数据），下面两个函数是修改外部数据源的VBA代码，调用它们就可以建立...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/excel'>Excel</a>  » </p><div class="series"><span>系列：<b>办公自动化</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/%e5%8a%9e%e5%85%ac%e8%87%aa%e5%8a%a8%e5%8c%96">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>Excel有一个很有用的功能是直接导入外部数据库或者使用外部数据源建立数据透视表和数据透视图。但比较可惜的是，这个数据源的查询语句是静态的，它无法根据日期自动修改（比如在应用中，我们希望每天获取的外部数据都是当天最新的数据），下面两个函数是修改外部数据源的VBA代码，调用它们就可以建立动态的数据源。</p>
<pre><span style="color: #008000;">' 更改数据表的来源</span>
<span style="color: #008000;">' wb：工作表对象</span>
<span style="color: #008000;">' connectionName：数据来源连接名称</span>
<span style="color: #008000;">' strSQL：新查询语句（修改SQL的查询代码）</span>
<span style="color: #008000;">' strSQLConnection：新连接语句（修改来源数据库，在对DBF数据库操作时非常有用）</span>
<span style="color: #008000;">' author: zhang@zhiqiang.org, 2010</span>
<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Sub</span> ChangeODBCConnection(wb <span style="color: #0000ff;">As</span> Excel.Workbook, connectionName <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">String</span>, _
        <span style="color: #0000ff;">Optional</span> strSQL <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">String</span> = "", <span style="color: #0000ff;">Optional</span> strSQLConnection <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">String</span> = "")

    <span style="color: #0000ff;">With</span> wb.Connections(connectionName).ODBCConnection
        <span style="color: #0000ff;">If</span> Len(strSQLConnection) <span style="color: #0000ff;">Then</span> .CommandText = SplitString(strSQLConnection)
        <span style="color: #0000ff;">If</span> Len(strSQL) <span style="color: #0000ff;">Then</span> .Connection = SplitString(strSQL)
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">With</span>
    wb.Connections(connectionName).Refresh
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Sub</span>

<span style="color: #008000;">' 更改数据表的来源</span>
<span style="color: #008000;">' pc：数据透视表的PivotCache对象 （例如：ActiveSheet.PivotTables(1).PivotCache）</span>
<span style="color: #008000;">' connectionName：数据来源连接名称</span>
<span style="color: #008000;">' strSQL：新查询语句（修改SQL的查询代码）</span>
<span style="color: #008000;">' strSQLConnection：新连接语句（修改来源数据库，在对DBF数据库操作时非常有用）</span>
<span style="color: #008000;">' author: zhang@zhiqiang.org, 2010</span>
<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Sub</span> ChangePivotConnection(pc <span style="color: #0000ff;">As</span> Excel.PivotCache, <span style="color: #0000ff;">Optional</span> strSQLConnect, _
        <span style="color: #0000ff;">Optional</span> strSQL <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">String</span> = "")
    <span style="color: #0000ff;">Dim</span> blnODBCConnect <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">Boolean</span>

    <span style="color: #0000ff;">With</span> pc
        <span style="color: #0000ff;">If</span> .QueryType = xlODBCQuery <span style="color: #0000ff;">Then</span>
            blnODBCConnect = <span style="color: #0000ff;">True</span>
            <span style="color: #0000ff;">If</span> Len(strSQLConnect) = 0 <span style="color: #0000ff;">Then</span> strSQLConnect = .Connection
            strSQLConnect = Replace(strSQLConnect, "<span style="color: #8b0000;">ODBC;DSN</span>", "<span style="color: #8b0000;">OLEDB;DSN</span>", 1, 1, vbTextCompare)
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

        <span style="color: #0000ff;">If</span> StrComp(.Connection, strSQLConnect, vbTextCompare) &lt;&gt; 0 <span style="color: #0000ff;">And</span> Len(strSQLConnect) <span style="color: #0000ff;">Then</span>
            .Connection = strSQLConnect
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

        <span style="color: #0000ff;">If</span> StrComp(.CommandText, strSQL, vbTextCompare) &lt;&gt; 0 <span style="color: #0000ff;">And</span> Len(strSQL) <span style="color: #0000ff;">Then</span>
            .CommandText = (strSQL)
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

        <span style="color: #0000ff;">If</span> blnODBCConnect = <span style="color: #0000ff;">True</span> <span style="color: #0000ff;">Then</span>
            .Connection = Replace(.Connection, "<span style="color: #8b0000;">OLEDB;DSN</span>", "<span style="color: #8b0000;">ODBC;DSN</span>", 1, 1, vbTextCompare)
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

        .Refresh
    <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">With</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Sub</span>

<span style="color: #008000;">' 将字符串分割成短字符串的数组</span>
<span style="color: #0000ff;">Private </span><span style="color: #0000ff;">Function</span> SplitString(<span style="color: #0000ff;">ByVal</span> s <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">String</span>) <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">Variant</span>
    <span style="color: #0000ff;">Dim</span> ss() <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">Variant</span>
    <span style="color: #0000ff;">Dim</span> i <span style="color: #0000ff;">As</span> <span style="color: #0000ff;">Long</span>

    <span style="color: #0000ff;">ReDim</span> ss(0 <span style="color: #0000ff;">To</span> Len(s) \ 200) <span style="color: #008000;">' note: it is not 256</span>
    <span style="color: #0000ff;">For</span> i = 0 <span style="color: #0000ff;">To</span> UBound(ss)
        ss(i) = Array(Mid(s, i * 200 + 1, 200))
    <span style="color: #0000ff;">Next</span> i

    SplitString = ss
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span></pre>
<p>以上代码在Office 2007版通过测试，其余版本未知。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/archive-email-in-outlook.html">Outlook中实现Gmail中的存档功能</a></li><li><a href="http://zhiqiang.org/blog/it/auto-save-attachment-in-outlook.html">自动保存Outlook邮件的附件</a></li><li><a href="http://zhiqiang.org/blog/it/auto-send-email-with-vbs.html">发送邮件的VBS脚本</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a></li><li><a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">用脚本添加ODBC数据源</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li><li><a href="http://zhiqiang.org/blog/it/refer-excel-worksheet-in-vba.html">VBA中引用WorkSheet的新方法</a></li><li><a href="http://zhiqiang.org/blog/it/get-stock-price-from-yahoo-in-excel.html">获取股票价格的Excel函数</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2010. | <a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html#comments">2 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/change-pivotcache-source.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>数据库查询是NP-Hard问题</title>
		<link>http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html#comments</comments>
		<pubDate>Tue, 22 Dec 2009 23:43:00 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[NP hard]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html</guid>
		<description><![CDATA[博客 » 计算机科学 » 数据库 » 问题来自美人他爹和Wangjianshuo's blog 一个查询的例子：NOT (AND ((C1&#62;5), OR ((C2&#60;6),(C3&#60;&#62;9)))) 问题1：给出两个这样的查询Q1和Q2，如何确定Q1的结果是否是Q2结果的子集？ 上面两篇文章主要是讨论实际工作中怎么解决问题1。下面从理论上分析一下这个问题。 问题2：判断Q1查询结果是否为空集。 在问题1中让Q2查询结果为空集，故问题1是比问题2...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href='http://zhiqiang.org/blog/tag/%e6%95%b0%e6%8d%ae%e5%ba%93'>数据库</a>  » </p><p>问题来自<a href="http://www.meirendaddy.com/blog/?p=694">美人他爹</a>和<a href="http://home.wangjianshuo.com/archives/20091213_i_am_not_computer_science_major.htm">Wangjianshuo's blog</a></p>
<p>一个查询的例子：NOT (AND ((C1&gt;5), OR ((C2&lt;6),(C3&lt;&gt;9))))</p>
<p>问题1：给出两个这样的查询Q1和Q2，如何确定Q1的结果是否是Q2结果的子集？</p>
<p>上面两篇文章主要是讨论实际工作中怎么解决问题1。下面从理论上分析一下这个问题。</p>
<p>问题2：判断Q1查询结果是否为空集。</p>
<p>在问题1中让Q2查询结果为空集，故问题1是比问题2更难的一个问题。</p>
<p>论断：问题2是<a href="http://zhiqiang.org/blog/tag/np-hard">NP-hard</a>的。</p>
<p>证明：从3SAT问题很容易规约到问题2。</p>
<p>结论：问题1是NP-Hard的，除非NP=P，不可能有多项式级别的解。也就是理论上而言，解决上面的问题除了枚举没有别的好方法了。</p>
<p>当然以上只是理论，实际应用中，<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_ed11dc9af96ff98159e9c06973c09457.gif' style='vertical-align: middle; border: none; ' class='tex' alt="3^n" /></span><script type='math/tex'>3^n</script>的计算速度和<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_d1db0d9c696a8c056e7117dbbb4ef6db.gif' style='vertical-align: middle; border: none; ' class='tex' alt="2^n" /></span><script type='math/tex'>2^n</script>的计算速度还是有很大的区别。如何解决NP-Hard问题也是一个被广泛研究的课题，毕竟实际工作中很多问题都是NP-Hard的。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/np-hard.html">TCS：NP-hard</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/minesweeper-is-np-complete.html">扫雷是NP完全问题</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/most-windows-game-are-np-complete.html">Windows游戏中的NP完全问题</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/introduce-probabilistic-checkable-proof.html">PCP - Probabilistic Checkable Proof</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a></li><li><a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">用脚本添加ODBC数据源</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2009. | <a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html#comments">2 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>加速blog：监测和优化WordPress数据库</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:39:01 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=730</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 在WordPress生成页面时，最消耗时间的便是数据库查询了。 监测WordPress的数据库查询 WordPress内置了数据库缓存系统，安装插件WordPress Cache Inspect，它会告诉你这个系统的效率（命中效率），主要为下面四个指标： Cold Cache Hits This is the number of cached items that were loaded from disk. Warm Cache Hits This is the number of cached items acc...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/%e7%bd%91%e7%ab%99%e6%8f%90%e9%80%9f'>网站提速</a>  » </p><div class="series"><span>系列：<b>网站提速</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/%e7%bd%91%e7%ab%99%e6%8f%90%e9%80%9f">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>在WordPress生成页面时，最消耗时间的便是数据库查询了。</p>
<h3>监测WordPress的数据库查询</h3>
<p>WordPress内置了数据库缓存系统，安装插件<a href="http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/">WordPress Cache Inspect</a>，它会告诉你这个系统的效率（命中效率），主要为下面四个指标：</p>
<dt><strong>Cold Cache Hits</strong> </dt>
<dd>This is the number of cached items that were loaded from disk. </dd>
<dt><strong>Warm Cache Hits</strong> </dt>
<dd>This is the number of cached items accessed that were already in memory </dd>
<dt><strong>Cache Misses</strong> </dt>
<dd>This is the number of items that had to be fetched from the db as they were not in the cache. </dd>
<dt><strong>Loaded data</strong> </dt>
<dd>This lists the type and amount of data loaded into memory from that currently stored within the cache..</p>
<h3>define('ENABLE_CACHE', true);</h3>
<p>把这条语句加入WordPress的配置文件wp-config.php里，WordPress便会以文本的方式缓存一些数据库查询的结果到/wp-content/cache/目录下。注意保持这个目录可写。</p>
<p>试试看效果再用。因为这玩意儿是用磁盘Cache保持数据库查询结果。但某些服务器上，磁盘IO比较慢，比如在dreamhost上就不推荐用这种方式。</p>
<h3>显示所有数据库查询语句和消耗时间</h3>
<p>在wp-config.php文件里添加<a style="color: #ffa500" href="http://cn.php.net/define">define</a>('<span style="color: #8b0000;">SAVEQUERIES</span>', <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=true&amp;lang=en&amp;scope=404quickref">true</a>);<span style="color: #0000ff;">，</span>再在footer.php文件的尾部加上一句<span style="color: #0000ff;">&lt;?</span>php <a style="color: #ffa500" href="http://cn.php.net/var_dump">var_dump</a>($wpdb-&gt;queries); <span style="color: #0000ff;">?&gt;</span> 。就可以在页面源代码的尾部找到整个页面执行过程中所提交的所有MySQL查询语句了。数组里每一项都包含一个string和一个float，string存储查询语句，float存储查询时间。</p>
<p>参考：查看WordPress页面执行过程中提交的SQL查询语句。</p>
<h3>比较耗时的数据库语句</h3>
<p><a href="http://www.21andy.com/blog/20060813/374.html">WordPress执行效率问题</a>作者作了一个测试，大家可以看一下具体有什么问题，与自己的做一个对照。我提一下我遇到过的比较慢的查询语句。</p>
<h4>分类和tag相关的都比较消耗时间</h4>
<p>比较消耗时间的基本上都与分类表相关的三个数据表相关。特别是我用的那个相关文章查询，基本上占总查询时间的20%（优化后）。我比较期待快速或者带Cache的相关文章的插件。</p>
<h4>DESC wp_comments</h4>
<p>我不知道这个语句干啥的，经常很慢，所以我把产生它的插件"subscribe comments"插件直接禁用了，反正那个插件提供的功能就没几个人用。</p>
</dd>
<p>持续更新ing...</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a></li><li><a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">用脚本添加ODBC数据源</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-your-blog.html">加速blog：序</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html">加速blog：减少和优化插件</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html#comments">2 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

