To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:
SELECT FLOOR(7 + (RAND() * 5));
文件自动增长和自动收缩
SQL Server允许用户设置数据库初始值、最大值,可以自动增长或者自动收缩。通过这些设置,可以防止数据库空间问题而导致的应用程序修改失败或者SQL Server把硬盘空间耗尽之类的事情发生。一般来讲,如果数据库不是很繁忙,默认的设置(开启自动增长)能够满足大部分的需求。但是数据文件和日志文件增长本身是一件耗费系统资源和影响性能的工作。所以如果完全依赖SQL Server自动完成,可能会导致系统性能不够稳定。一个管理得比较精细的系统,应该预先考虑到可能的空间使用需求,提前规划并引导数据的流向。尽量避免空间用尽而使得SQL Server不得不自动增长的现象发生。同时也要确保每一次自动增长都能够在可接受的时间内完成,及时满足客户端应用的需求。
那么怎么才能达到这样的目的呢?在谈论最佳配置之前,首先要讨论一下SQL Server数据文件和日志文件空间申请的一些特点。还是以下面这个数据库(如图1-31所示)为例。它有3个数据文件(假设它们属于同一个文件组)和两个日志文件(见表1-7): Continue reading ‘文件自动增长和自动收缩SQL Server’

Replication Mysql
mysql主从热备有2种配置方式,备份某些库或者忽略备份某些库,建议选择后者。
Continue reading ‘MySQL主从热备配置(含innodb)’
- 基本使用:
1
| SELECT * FROM `table` ORDER BY RAND() LIMIT 0,10; |
但是在ORDER BY后面用到RAND(),查询速度非常慢.
- MAX()*RAND()提高效率
1 2 3
| SELECT * FROM `table`
WHERE id > ;= (SELECT floor( RAND() * ((SELECT MAX(id ) FROM `table` )-(SELECT MIN(id ) FROM `table` )) + (SELECT MIN(id ) FROM `table` )))
ORDER BY id LIMIT 1; |
- JOIN
1 2 3 4
| SELECT *
FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id ) FROM `table` )-(SELECT MIN(id ) FROM `table` ))+(SELECT MIN(id ) FROM `table` )) AS id ) AS t2
WHERE t1 .id > ;= t2 .id
ORDER BY t1 .id LIMIT 1; |
Continue reading ‘提高mysql随机查询的效率’
Published on
July 20, 2008 in
Tech.
Tags: MySQL.
管理 MySQL 最讓人困擾的就是如何有效的掌握 MySQL 的健康狀況,因為 MySQL 雖然有提供許多系統變數值供您參考,但這些零散的數據若要手動搜集與過濾將會是一件十分沒有效率的事情(除非您寫 Scripts 去分析)。而接下來要介紹的這套 “工具” 其實是由 hackmysql.com 的站長所撰寫的 Perl Scritps,旨在協助 MySQL DBA 搜集與分析 MySQL 的運作狀況。
连接MySQL得到这样的错误提示
1
| Warning: mysql_pconnect(): Host '67.19.xx.xxx' is blocked because of many connection errors. Unblock with 'mysqladmin flush-hosts' in C:\Websites\ratedesi\include\connectdb.php on line 9 Host '67.19.xx.xxx' is blocked because of many connection errors. Unblock with 'mysqladmin flush-hosts' |
在MySQL手册中找到这样的说明
A.2.5 `Host ‘HOST_NAME’ is blocked’
————————————
If you get the following error, it means that `mysqld’ has received
many connect requests from the host `’HOST_NAME that have
been interrupted in the middle:
Host ‘HOST_NAME’ is blocked because of many connection errors.
Unblock with ‘mysqladmin flush-hosts’
The number of interrupted connect requests allowed is determined by
the value of the `max_connect_errors’ system variable. After
`max_connect_errors’ failed requests, `mysqld’ assumes that
something is wrong (for example, that someone is trying to break
in), and blocks the host from further connections until you execute
a `mysqladmin flush-hosts’ command or issue a `FLUSH HOSTS’
statement. See *Note server-system-variables::.
By default, `mysqld’ blocks a host after 10 connection errors. You
can adjust the value by starting the server like this:
shell> mysqld_safe –max_connect_errors=10000 &
If you get this error message for a given host, you should first
verify that there isn’t anything wrong with TCP/IP connections from
that host. If you are having network problems, it does you no good
to increase the value of the `max_connect_errors’ variable.
这可能是比如PHP的未注销连接引起,查看MySQL的进程可以查看一下
1
| mysqladmin -u <user> -p<password> processlist |
以 apache handler 和 fastcgi 两种方式安装 PHP 5.2.0, 并使用 –with-mysql 和 –with-mysqli 两种方式连接 Mysql 数据库
- 配置 PHP 安装
1
| './configure' '--codefix=/opt/php5' '--with-apxs2=/opt/bin/apxs' '--enable-fastcgi' '--with-mysql=/usr/local/mysql' '--enable-calendar' '--enable-force-cgi-redirect' '--with-configure-file-path=/opt/php5/cgi' '--enable-trans-sid' '--with-gd' '--with-xml' '--with-ttf=/usr' '--with-freetype-dir=/usr' '--enable-exif' '--with-dom-xslt=/usr' '--with-jpeg-dir=/usr/local/jpeg6b' '--with-png-dir=/usr' '--with-zlib-dir=/usr' '--enable-ftp' '--with-curl=/usr' '--enable-mbstring' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-openssl=/usr' '--with-gettext' '--enable-sockets' '--enable-memory-limit' '--with-xmlrpc' '--enable-debug=no' '--enable-track-vars' '--with-iconv' '--enable-sqlite-utf8' '--with-pgsql=/opt/postgresql' |
- 这里我还安装了 Postgresql 所以使用了 ‘–with-pgsql=/opt/postgresql’ ,不过奇怪的是 如果我使用
1
| --with-pdo-mysql --with-pdo-pgsql |
就会出现配置安装的错误。
- 同时使用 mysql 和 mysqli 在 make 的时候可能会出现很多警告,在 MAKEFILE 文件中找到以 EXTRA_LIBS 开始的行,删除第二个 -lmysqlclient_r
Recent Comments