• Resolved arnevanhoorn

    (@arnevanhoorn)


    We’ve been using the plugin for some years now. Recently the site ran into a lot of memory issues. I investigated what was going on and I noticed that the wp_tinvwl_lists table contained over a million records. I then looked at the code of the plugin and saw a heavy piece of code:

    function unique_share_key()
    	{
    		global $wpdb;
    
    		$sharekeys = $wpdb->get_results("SELECT <code>share_key</code> FROM <code>{$this->table}</code>", ARRAY_A); // WPCS: db call ok; no-cache ok; unprepared SQL ok.
    		$share_keys = array();
    		foreach ($sharekeys as $sharekey) {
    			$share_keys[] = $sharekey['share_key'];
    		}
    		$new_key = '';
    		do {
    			$new_key = substr(md5(date('r') . mt_rand(0, 3000)), 0, 6);
    		} while (array_search($new_key, $share_keys)); // @codingStandardsIgnoreLine WordPress.PHP.StrictInArray.MissingTrueStrict
    
    		return $new_key;
    	}

    I think this function should be rewritten to be less heavy, something like this:

    function unique_share_key()
    {
        global $wpdb;
    
        $test_key = uniqid();
        $unique = false;
        while ($unique === false) {
            $unique = !$wpdb->get_var($wpdb->prepare("SELECT ID FROM <code>{$this->table}</code> WHERE share_key = %s", $test_key));
            $test_key = uniqid();
        }
    
        return $test_key;
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘wp_tinvwl_lists – too many records’ is closed to new replies.