PDO create tableで4000個テーブルを作る時間

銘柄リストから、それぞれのテーブルを作っていきます。sqlにはif not existsを入れます。

$time_start = microtime(true);
$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 

$sql = "select code from lists";
$stmt = $dbh->query($sql);
while($result = $stmt->fetch(PDO::FETCH_ASSOC)){
		$code[] = $result["code"];
}


foreach($code as $value){
	$sql = "create table if not exists equity.code".$value."(
		id int unsigned auto_increment primary key,
		date int,
		close int
	)ENGINE = MYISAM;";
	$stmt = $dbh->query($sql);
}

$time = microtime(true) - $time_start;
echo "{$time}秒";

mysql側 入ってます。

経過時間:35秒

問題は次ですが、
アップデートがなかった場合:11.73秒

localで走らせるので、そこまで大きな問題ではなさそうですね。
一からもう一度やってみましたが、大体同じ時間です。

試しにgoogle financeから1個データを入れてみます。

これは。。。かなりやる気なくなってきた。

PDOでcreate table

$time_start = microtime(true);
$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 

$code= '2338';

$sql = "create table equity.code".$code."(
	id int unsigned auto_increment primary key,
	date int,
	close int
)ENGINE = MYISAM;";
$stmt = $dbh->query($sql);

$time = microtime(true) - $time_start;
echo "{$time}秒";

mysql 重複を除いた値の一覧抽出

以下で取得可能です。
SELECT DISTINCT colname FROM tablename;

mysql> select distinct sector from lists;
+————————–+
| sector |
+————————–+
| 水産・農林業 |
| 卸売業 |
| 建設業 |
| 非鉄金属 |
| 鉱業 |
| 機械 |
| サービス業 |
| 金属製品 |
| 情報・通信 |
| 食料品 |
| 医薬品 |
| 不動産業 |
| 陸運業 |
| その他金融業 |
| 小売業 |
| その他製品 |
| 繊維製品 |
| 電気機器 |
| ガラス・土石製品 |
| 証券業 |
| 輸送用機器 |
| REIT銘柄一覧 |
| 石油・石炭製品 |
| 化学 |
| パルプ・紙 |
| 精密機器 |
| ゴム製品 |
| 鉄鋼 |
| 銀行業 |
| 保険業 |
| 倉庫・運輸関連業 |
| 海運業 |
| 空運業 |
| 電気・ガス業 |
+————————–+
34 rows in set (0.05 sec)

早く気づけばよかった。

DBから同業種・同じ市場の銘柄を抽出する

select * from table where で条件を記載します。

$code = empty($_GET["code"])? 'null' : $_GET["code"];

$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 


$sql = "select * from lists where code = $code";
$stmt = $dbh->query($sql);

$result = $stmt->fetch(PDO::FETCH_ASSOC);
$sector = $result['sector'];
$market = $result['market'];

echo "<b>".$result['name']. "</b>(".$code.") ";
echo $result['sector'] ." ".$result['market']. "<br><br>";
echo "同業種の銘柄(".$sector.")"."<br>";

$sql2 = "select * from lists where market = '$market' AND sector = '$sector' and code != $code";
$stmt2 = $dbh->query($sql2);
while($result = $stmt2->fetch(PDO::FETCH_ASSOC)){
	    echo $result["code"]." ".$result["name"]. "<br>";
}

パラメータを$_GETで受け取る

echo $_GET["code"];

コードの情報をDBから取得する

$time_start = microtime(true);

$code = empty($_GET["code"])? 'null' : $_GET["code"];
echo $code. "<br>";

$dsn = "mysql:dbname=equity;host=localhost";
$user = "hoge";
$password = "hogehoge";
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e){
    print('connection failed:'.$e->getMessage());
} 

$sql = "select * from lists where code = $code";
$stmt = $dbh->query($sql);

$result = $stmt->fetch(PDO::FETCH_ASSOC);
 
echo $result['name']. "<br>";
echo $result['sector']. "<br>";
echo $result['market']. "<br>";

echo "<br>";
$time = microtime(true) - $time_start;
echo "{$time}秒";

OK!

パラメータからデータを読み込む

パラメータを変える

なるほど

さくら共有サーバー cake3.5インストール後の.htaccessの設定

www/cake/に testというappを作っています。
それぞれのhtaccessのRewriteBaseでカレントディレクトリを指定してあげます。

/cake/.htaccess

  
   RewriteEngine on  
   RewriteRule    ^$ test/webroot/    [L]  
   RewriteRule    (.*) app/webroot/$1 [L]  
   RewriteBase /  
  

/cake/test/.htaccess

  
    RewriteEngine on  
    RewriteRule    ^$    webroot/    [L]  
    RewriteRule    (.*) webroot/$1    [L]  
    RewriteBase /test
  

/cake/test/webroot/.htaccess


    RewriteEngine On
    RewriteBase /test/webroot
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

きたー

ここまでもってくるの、結構煩雑だな~
VPSの方が簡単にセットアップできるという謎現象ですね。

さくらサーバーでintlを入れた後にcakephp3.5エラー

intlも入ったし、さあ、確認とおもったら、

php composer.phar create-project --prefer-dist cakephp/app myapp

Problem 1
– cakephp/cakephp 3.5.9 requires ext-intl * -> the requested PHP extension intl is missing from your system.

これはがっかりしますね。
確かに、php -i | grep intlで何も表示されない。

原因は、この php.ini は/usr/local/php/5.6/lib/php.iniの方を読み込んでいるから。
コントロールパネルで設定したphp.iniの方は、

php -c /home/[username]/www/php.ini -i | grep intl 

で表示される。

というわけで、cake3.5を入れる際も、以下のように打ちます。

php -c /home/[username]/www/php.ini composer.phar create-project --prefer-dist cakephp/app test

こんどは上手くいってるようです。

さくら共有サーバーにintlを入れて、cakephp3.5をインストール

intlがないと動きません。
というこで、まずicuライブラリを入れます。ここはサクサクいきます。

$ cd ~/local/src
$ wget http://download.icu-project.org/files/icu4c/54.1/icu4c-54_1-src.tgz
$ tar zxvf icu4c-54_1-src.tgz
$ cd icu/source
$ ./configure --prefix=$HOME/local
$ gmake
$ gmake install

続いて、intl

$ cd ~/local/src
$ wget http://pecl.php.net/get/intl-3.0.0.tgz
$ tar zxvf intl-3.0.0.tgz
$ cd intl-3.0.0
$ phpize

次に、.configureをしますが、phpのバージョンを指定します。
/php/5.6/
ここでphpのバージョンを指定しないと、エラーになりました。

$ ./configure --prefix=$HOME/local --with-icu-dir=$HOME/local --with-php-config=/usr/local/php/5.6/bin/php-config
$ make

あとは、php.initでextensionのパスを通します。
phpinfo()に入ってることを確認。

バージョン指定のところ、毎回見落とすんだよな~
自暴自棄です。

centos php7.0.27にintlを入れようとして嵌ったこと

まず、intlを入れようとします。

# sudo yum install --enablerepo=remi --enablerepo=remi-php56 -y php-intl

すると、phpが7.0.27とエラー表示されます。

Error: Package: php-intl-5.6.34-1.el6.remi.x86_64 (remi-php56)
           Requires: php-common(x86-64) = 5.6.34-1.el6.remi
           Installed: php-common-7.0.27-1.el6.remi.x86_64 (@remi-php70)
               php-common(x86-64) = 7.0.27-1.el6.remi
           Available: php-common-5.3.3-49.el6.x86_

phpのバージョン確認

# php -v
PHP 7.0.27 (cli) (built: Jan  2 2018 12:12:41) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

intlのリストを表示

# yum list | grep intl
intltool.noarch                            0.41.0-1.1.el6              @anaconda       -CentOS-201703281317.x86_64/6.9
perl-libintl.x86_64                        1.20-1.el6                  base
php-intl.x86_64                            5.3.3-49.el6                base
php-symfony-intl.noarch                    2.3.42-1.el6                epel
php54-php-intl.x86_64                      5.4.45-14.el6.remi          remi-safe
php55-php-intl.x86_64                      5.5.38-8.el6.remi           remi-safe
php56-php-intl.x86_64                      5.6.34-1.el6.remi           remi-safe
php70-php-intl.x86_64                      7.0.28-1.el6.remi           remi-safe
php71-php-intl.x86_64                      7.1.15-1.el6.remi           remi-safe
php72-php-intl.x86_64                      7.2.3-1.el6.remi            remi-safe

php70-php-intl.x86_64 を入れます。

# sudo yum install php70-php-intl

はいったか確認します。

# php -i | grep intl
#

なに!? 入ってない。
/opt/remi/php70/root/usr/lib64/php/modules/ に入っている。

/usr/lib64/php/modules にintl.soを入れる。

phpinfo()

コマンドライン

# php -i | grep intl
intl
intl.default_locale => no value => no value
intl.error_level => 0 => 0
intl.use_exceptions => 0 => 0

ふー焦った。

optはアドオンアプリケーションソフトウェアパッケージ(追加アプリケーション)
remiサードパーティのリポジトリの一つ とのこと。